HOW TO HIGHLIGHT CELL IN A GRID WHICH MATCHES SEARCH RESULT?

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
 2344  View(s)
Ratings:
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. 
  1. Pravesh Singh

    Post:101

    Points:709
    Re: How to Highlight Cell in a grid which matches search result?

    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 };
                        }                   
    }

  1. Horas Panjaitan

    Post:3

    Points:21
    Re: How to Highlight Cell in a grid which matches search result?


    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

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!