I have the following code :
private void dgvStatus_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in dgvStatus.Rows)
{
row.Cells[color.Index].Style.BackColor = Color.FromArgb(((GesTest.dsEssais.FMstatusAnomalieRow)row.DataBoundItem).iColor);
}
}
I am trying to set the background color of each cell from the background color column. this doesn't work the color never change. Any idea of why?
I've been looking around but didn't found anything usefull
Similar as shown and mentioned:
Notes: Take into consideration that Cells will change their color (only) after the DataGridView Control is Visible. Therefore one practical solution would be using the:
In case you wish to keep your style when creating new Rows; also subscribe the:
Example bellow:
Finally: Just call the DataGridView_Configuration() (method)
i.e: On Form Load).
If you are still intrested in why this didn't work for you at first:
The reason you don't see changes you've made to the cell's style is because you do these changes before the form was shown, and so they are disregarded.
Changing cell styles in the events suggested here will do the job, but they are called multiple times causing your style changes to happen more times than you wish, and so aren't very efficient.
To solve this, either change the style after the point in your code in which the form is shown, or subscribe to the Shown event, and place your changes there (this is event is called significantly less than the other events suggested).
Simply create a new DataGridViewCellStyle object, set its back color and then assign the cell's style to it:
You can use the
VisibleChanged
event handler.I finally managed to get it working. Here the code :
if anyone know a better to do this please don't hesitate to post it. I'm open to suggestion