C# 根据指定列中的值高亮显示winforms中GridView控件中的行

C# 根据指定列中的值高亮显示winforms中GridView控件中的行,c#,winforms,gridview,C#,Winforms,Gridview,如果列中的值匹配,我尝试使用C#突出显示WinForms Gridview中的整行 指定的字符串值: 我正在使用: private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { if (Convert.ToString(dataGridView1.Rows[e.RowIndex]

如果列中的值匹配,我尝试使用C#突出显示WinForms Gridview中的整行 指定的字符串值:

我正在使用:

 private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
           
               
            if (Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[5]).Contains("Product charges"))
            {
                dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Beige;
            }

        }
但它不起作用

我还尝试:

foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if ((Convert.ToString(cell.Value) == "Product charges"))
                    {
                        row.InheritedStyle.BackColor = Color.BlueViolet;
                    } 
                }
            }
我试着:

private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {


            //if (Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[5]).Contains("Product charges"))
            //{
            //    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Beige;
            //}

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if ((Convert.ToString(cell.Value).Contains("Product charges")))
                    {
                        row.InheritedStyle.BackColor = Color.BlueViolet;
                    } 
                }
            }

        }
我知道我错过了一些简单的事情

  foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            foreach (DataGridViewCell cell in row.Cells)
            {
                if ((Convert.ToString(cell.Value).Contains("2")))
                {
                    row.DefaultCellStyle.BackColor = Color.BlueViolet;
                }
            }
        }
这应该对你有用

检查您的代码是否满足以下条件

 if ((Convert.ToString(cell.Value).Contains("Product charges")))

您能在特定列中指示一个值吗?如果您在
If
条件块中设置了断点,它会被命中吗?另外,使用列名而不是索引(如
dataGridView1[“ColumnName”,e.RowIndex].Value.ToString()
而不是
dataGridView1.Rows[e.RowIndex].Cells[5]