I have set the first column of a DataGridView
as CheckBoxColumn
, when activating the checkBox
, I would like it to paint the whole row, in fact it works for me, but it doesn't seem to apply the changes until I exit that row. I leave you the code that I have to see if you can give me a cable, I have tried with dataGrid.Refresh()
and .Update()
, but I don't get anything. Thank you very much.
private void dgvComandes_MouseUp(object sender, MouseEventArgs e)
{
dgvComandes.EndEdit();
}
private void dgvComandes_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if(e.ColumnIndex == dgvComandes.Columns["colCheck"].Index)
{
bool sel = (bool)dgvComandes.Rows[e.RowIndex].Cells["colCheck"].Value ;
if(sel)
{
dgvComandes.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.FromArgb(106, 174, 144);
dgvComandes.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.White;
}
else
{
dgvComandes.Rows[e.RowIndex].DefaultCellStyle.ForeColor = dgvComandes.DefaultCellStyle.ForeColor;
dgvComandes.Rows[e.RowIndex].DefaultCellStyle.BackColor = dgvComandes.DefaultCellStyle.BackColor;
}
}
}
Put this code in the CellValueChanged event of the DataGridView
What this code does is that it asks if we are standing on the checkbox column, if it is true we save the row in the variable row and cast the cell that has the checkbox to DataGridViewCheckBox. Now we ask if the checkbox is marked or not, depending on whether it is marked or not, we put a certain color.
So far so good, but the color change is not accepted when checking or unchecking the checkbox but when we select another cell. To solve this we go to the CurrentCellDirtyStateChanged event of the DataGridView and put this code
Basically what is done is to ask if there is any pending change and if so, we update.