简体   繁体   中英

How to check the row color in DataGridView in C#

I want to get the DataGridView row color in C#.

I have set the background color of a row like this:

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

Now I want to get only those rows whose BackgroundColor is LightPink .

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

    }
}

Like this:

int index = 0;

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

    }
    index++;
}

You were close. However, note that dgvGrid.Rows is not a collection of DataRow - which:

Represents a row of data in a DataTable.

Instead, it is a DataGridViewRowCollection which is:

A collection of DataGridViewRow objects.

After fixing this in your loop, just check the row color the same way you set it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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