views:

1395

answers:

3

Hi,

I am trying to set the background color of a DataGridView row to red. I tried with the following line:

dgvActiveCalls.Rows[1].DefaultCellStyle.BackColor = Color.Red;

but it doesn't work. Then just to see if something is wrong with update I tried to paint the column instead of row:

dgvActiveCalls.Columns[1].DefaultCellStyle.BackColor = Color.Red;

and it worked fine. I would be really thankful if anyone could show the way to paint the DataGridView row.

Thanks!

A: 

I tested that on my machine and it worked fine. Are you sure that line isn't highlighted? If it's highlighted you'll see the highlighted color, not the red.

BFree
I'm sure I tested it again...
niko
+1  A: 

Make sure that the default style isn't being overriden by another of the styles. Interesting quirk of DGV in 2.0: Seems like the inheritence chain is almost upside down from what you'd expect. If you are dynamically adding columns, your DefaultCellStyle can be ignored and be overridden by the RowsDefaultCellStyle. Sorting can also override styles you've set.

You might want to check in which order you're setting these styles and google for some articles regarding style inheritence.

P.S. Interestingly enough, I googled to find a link to provide, and came across this blog with an almost identical explanation:

http://yakkowarner.blogspot.com/2008/06/datagridview-style-inheritance.html

joshua.ewer
A: 

This is really strange. I tested the same function on coworker's machine and it worked fine and it still doesn't work on my machine.

niko