Home > DeveloperSection > Forums > How to Highlight Cell in a grid which matches search result?
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    October-21-2014 12:51 AM

 ASP.Net ASP.Net  Grid 
Ratings:


 2 Reply(s)
 2025  View(s)
Rate this:
Dim DataGridView1 = New DataGridView
      'add columns to DGV
      DataGridView1.Columns.Add("Year", "Year")
      DataGridView1.Columns.Add("Make", "Make")
      DataGridView1.Columns.Add("Model", "Model")
      DataGridView1.Columns("Year").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
      DataGridView1.Columns("Make").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
      DataGridView1.Columns("Model").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
      DataGridView1.RowHeadersVisible = False
 
      'add items to DGV
      DataGridView1.Rows.Add("2010", "Chevrolet", "Camaro")
      DataGridView1.Rows.Add("2006", "Ford", "Mustang")
      DataGridView1.Rows.Add("2008", "Dodge", "Charger")
      DataGridView1.Rows.Add("2007", "Chevrolet", "Corvette")
In this Datagridview if i type "ro" in a text box i need any column that having "ro" (eg: Chevrolet,Camaro ) should be highlight. 


Pravesh Singh
Pravesh Singh

Total Post:101

Points:709
Posted on    October-21-2014 12:54 AM

for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if (dataGridView1.Rows[i].Cells["Make"].Value.ToString().ToUpper().Contains("ro"))
                    {
                        dataGridView1.Rows[i].Cells["Make"].Style = new DataGridViewCellStyle() { Font = new Font(dataGridView1.Font, FontStyle.Bold), ForeColor = Color.DodgerBlue };
//it will change cell forecolor you can use according to you required action
// same like forecolor you can change backcolor or cell and also row.

                    }
//ro 
// or you can use textbox value.
                    if (dataGridView1.Rows[i].Cells["Model"].Value.ToString().ToUpper().Contains(txtBox1.Text))
                    {
                        dataGridView1.Rows[i].Cells["Model"].Style = new DataGridViewCellStyle() { Font = new Font(dataGridView1.Font, FontStyle.Bold), ForeColor = Color.DodgerBlue };
 
// for full row colors you can use following
// dataGridView1.Rows[i].DefaultCellStyle = new DataGridViewCellStyle() { Font = new Font(dataGridView1.Font, FontStyle.Bold), ForeColor = Color.DodgerBlue, BackColor = Color.Green };
                    }                   
}

Horas Panjaitan

Total Post:3

Points:21
Posted on    November-06-2014 2:04 AM


hi Pravesh Singh

how to filter records gridview dropdownlist using mysql database


what if it does not use SqlDataSource.

I populate and retrieve data from code behind

please help me



Don't want to miss updates? Please click the below button!

Follow MindStick