【问题标题】:How to check the row color in DataGridView in C#如何在 C# 中检查 DataGridView 中的行颜色
【发布时间】:2016-05-31 15:09:45
【问题描述】:

我想在 C# 中获得 DataGridView 行颜色。

我已经像这样设置了一行的背景颜色:

dgvGrid.Rows[rowIndex].DefaultCellStyle.BackColor = Color.LightPink;

现在我只想获取BackgroundColorLightPink 的那些行。

foreach (DataRow dr in dgvGrid.Rows)
{
    if( /* get the row whose color is pink */)
    {

    }
}

【问题讨论】:

    标签: c# windows winforms datagridview


    【解决方案1】:

    像这样:

    int index = 0;
    
    foreach (var item in ListGV.Rows)
    {        
        if (ListGV.Rows[index].BackColor == Color.Pink)                
        {
    
        }
        index++;
    }
    

    【讨论】:

    • foreach 在这里毫无意义... item 甚至没有被使用,需要额外的索引 -> for 会更好.. #BetterUndoUsefulEdits
    • 好吧,在这里做出你自己的答案,而不是成为一个聪明的驴子
    • 本网站旨在帮助他人为他们的问题找到最佳解决方案。它的意义是获得满足所有需求的社区结果,而不是仅仅因为人们想做自己的事情而获得各种相似的答案。具有相同内容的单独答案将毫无意义...
    【解决方案2】:

    你很亲密。但是,请注意dgvGrid.Rows 不是 DataRow 的集合 - 其中:

    Represents a row of data in a DataTable.

    相反,它是DataGridViewRowCollection,即:

    A collection of DataGridViewRow objects.

    在循环中修复此问题后,只需以与设置相同的方式检查行颜色:

    foreach (DataGridViewRow row in dgvGrid.Rows)
    {
        if (row.DefaultCellStyle.BackColor == Color.LightPink)
        {
            // your code here
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 2012-09-08
      • 2014-07-19
      相关资源
      最近更新 更多