I am giving color to the row of DataGridView if the cell box is selected, I have created a function which I invoke in the event CellValueChanged of DataGridView works fine, but only when it is the user that check the box, if the box is selected from the database, do not color the row.
Now I have done it with the event
CellFormattingbut it keeps running at all times and in the interface the cells are shown Blinking at all times during the execution time. Is there any other way to do it without using the CellFormatting event?
Selected Data from the Database:
As you can see they are selected, but the rows do not take the color.
When the user Check / Uncheck the box:
Function:
private void colorFila(DataGridView datagrid)
{
foreach (DataGridViewRow row in datagrid.Rows)
{
if (Convert.ToBoolean(datagrid.Rows[row.Index].Cells[0].Value) == true)
{
row.DefaultCellStyle.BackColor = Color.SeaGreen;
row.DefaultCellStyle.SelectionBackColor = Color.SeaGreen;
}
else
{
row.DefaultCellStyle.BackColor = Color.White;
row.DefaultCellStyle.SelectionBackColor = Color.RoyalBlue;
}
}
}
Event:
private void dgridDepartamentos_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
colorFila(dgridDepartamentos);
}
I have created a function because I have several
DataGridViewsimilar to be able to reuse the same function.
Environment: Visual Studio 2010 (WindowsForms) & .NET NetFramework 4