繁体   English   中英

如何在C#中的DataGridView中检查行颜色

[英]How to check the row color in DataGridView in C#

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

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

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

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

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

    }
}

像这样:

int index = 0;

foreach (var item in ListGV.Rows)
{        
    if (ListGV.Rows[index].BackColor == Color.Pink)                
    {

    }
    index++;
}

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

表示DataTable中的一行数据。

而是一个DataGridViewRowCollection ,它是:

DataGridViewRow对象的集合。

在循环中解决此问题后,只需按照设置方式检查行颜色即可:

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

暂无
暂无

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

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