根据指定列中的值在winforms中突出显示GridView contron中的一行

时间:2021-04-24 14:23:24

标签: c# winforms gridview

如果列中的值匹配,我正在尝试使用 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;
                    } 
                }
            }

        }

我知道我遗漏了一些简单的东西。

1 个答案:

答案 0 :(得分:0)

  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")))
相关问题