(C#) Format the cell of DataGridView based on the TextBox.Text

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.Font = new Font(dataGridView1.Font.FontFamily, this.Font.Size, FontStyle.Bold);
            style.BackColor = Color.Yellow;//any style as you want;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (cell.Value != null && !string.IsNullOrEmpty(this.textBox1.Text) && cell.Value.ToString().Contains(this.textBox1.Text))
                    {
                        cell.Style = style;
                    }
                    else
                        cell.Style = dataGridView1.DefaultCellStyle;
                }
                
            }
        }

 Result:

 

posted @ 2015-03-27 17:36  Youjun  阅读(221)  评论(0编辑  收藏  举报