To change row color in DataGridView name dataGridFact we have that see CellValueChanged apply color .
private void dataGridFact_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dataGridFact.Columns[e.ColumnIndex].Name == "Check" && dataGridFact.CurrentCell is DataGridViewCheckBoxCell)
{
bool isChecked = (bool)dataGridFact[e.ColumnIndex, e.RowIndex].EditedFormattedValue;
var row = this.dataGridFact.Rows[e.RowIndex];
if (isChecked)
{
row.DefaultCellStyle.BackColor = Color.Bisque;
row.DefaultCellStyle.SelectionBackColor = Color.Bisque;
}
else
{
row.DefaultCellStyle.BackColor = Color.White;
row.DefaultCellStyle.SelectionBackColor = Color.White;
}
dataGridFact.EndEdit();
}
}
And then call CommitEdit in CurrectCellDirtyChanged to apply color.
private void dataGridFact_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridFact.CurrentCell is DataGridViewCheckBoxCell)
{
dataGridFact.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
Adalberto Montanía
private void dataGridFact_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dataGridFact.Columns[e.ColumnIndex].Name == "Check" && dataGridFact.CurrentCell is DataGridViewCheckBoxCell)
{
bool isChecked = (bool)dataGridFact[e.ColumnIndex, e.RowIndex].EditedFormattedValue;
var row = this.dataGridFact.Rows[e.RowIndex];
if (isChecked)
{
row.DefaultCellStyle.BackColor = Color.Bisque;
row.DefaultCellStyle.SelectionBackColor = Color.Bisque;
}
else
{
row.DefaultCellStyle.BackColor = Color.White;
row.DefaultCellStyle.SelectionBackColor = Color.White;
}
dataGridFact.EndEdit();
}
}
And then call CommitEdit in CurrectCellDirtyChanged to apply color.
private void dataGridFact_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridFact.CurrentCell is DataGridViewCheckBoxCell)
{
dataGridFact.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
Adalberto Montanía
Comentarios
Publicar un comentario