How to Highlight Cell in a grid which matches search result?

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. 
Last updated:11/6/2014 2:04:37 AM

2 Answers

Anonymous User
Anonymous User

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
Horas Panjaitan

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


Answer