简体   繁体   English

检查datagridview C#行的颜色

[英]Check color of the rows of the datagridview c#

I basically want to check if a certain row contains white color then it should not be replaced in the datagridview otherwise if it contains any other color then the text in that row can be replaced but I don't know how to check that condition in this scenario. 我基本上想检查某一行是否包含白色,那么它不应在datagridview中替换,否则,如果它包含任何其他颜色,则可以替换该行中的文本,但是我不知道如何在此检查该条件场景。

This is the code. 这是代码。

private void button9_Click_1(object sender, EventArgs e)
{
  var original = ((DataTable)dataGridView1.DataSource);
  var clone = original.Clone();
  var ordinal = original.Columns["Stringtext"].Ordinal;
  for (int i = 0; i < original.Rows.Count; i++)
  {
     var values = original.Rows[i].ItemArray;
            values[ordinal] = ((values[ordinal].ToString()).ToLower())
                .Replace(textBox6.Text.ToLower(), textBox7.Text);
            clone.Rows.Add(values);
  }
        dataGridView1.DataSource = clone;
        string filterBy;
        filterBy = "Stringtext Like '%" + textBox7.Text + "%'";
        ((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = filterBy;
}

any ideas? 有任何想法吗?

To loop through all cells in the DataGridView and check the background color of the cell you can do something like 要遍历DataGridView所有单元格并检查单元格的背景色,您可以执行以下操作

for (int c = 0; c < dataGridView.ColumnCount; c++)
{
    for (int r = 0; r < dataGridView.RowCount; r++)
    {
        if (dataGridView.DefaultCellStyle.BackColor != Color.White)
            // Do your update here
    }
}

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM