简体   繁体   中英

How to check if a DataGridView cell has a custom BackColor

I have a DataGridView loaded with data and some cells have been highlighted with a custom BackColor. I need to check if a cell is highlighted (i don't know the higlight color in advance) to execute some code. I am using the following code:

Color CellColor = MyDataGrid.Rows[r].Cells[c].Style.BackColor;
//if (CellColor != Color.FromArgb(0,0,0,0)) {
//if (CellColor != Color.FromName("0")) {
if (CellColor != MiGrid.DefaultCellStyle.BackColor) {
    // Do something
}

I asume the not highlighted cells have the DefaultCellStyle.BackColor but it doesn't work. Since the DefaultCellStyle.BackColor returns Color.FromARGB(0, 0, 0, 0) I also tried comparing to that but it still doesn't work. Can anyone show me how to check if the cell doesn't have a custom Backcolor?

Thanks to all the folks that tryied to help. Fortunatelly I found out the answer myself: a DataGridView cell's default Backcolor is Color.Empty . If you check the Style.BackColor property of the cell against Color.Empty you can identify if the cell has been assigned a custom background color.

First, it's not apparent what environment are you using. From the context I would say that it's a Winforms and default DataGridView .

Next, what do you mean by highlighted ? I get a notion, that you're confusing a bit selection and styling . The question whether you are after selecting or styling of the cell is important.

Providing you're after styling, the default cell color in winforms is:

SystemColors.Window

Even in the case that the Windows environment would use customized colors, this should wokrk. You can query against that color or set it to the cells.

If you want to find out what cells are selected, that might get a bit complicated. First, you have to ensure what mode the selection is in. Typical are CellSelect , RowHeaderSelect or FullRowSelect . The aim and mode of selection would have to be clarified.

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