views:

33

answers:

1

I have a DataGridView bound to a list of objects, and I'm setting a dynamic cell background colour using the CellFormatting event, as in this answer. This works well for every column except the DataGridViewCheckboxColumn. When I click inside this cell (but outside the checkbox) the cell background changes to the default white.

Visually it looks like cell selection is occurring, despite my best efforts to stop it. My cell formatting code sets the SelectionBackColor as well as the BackColor. I've disabled cell selection using the CellStateChanged event, and none of the other columns are selectable:

private void PlayerGrid_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
    if (e.StateChanged == DataGridViewElementStates.Selected)
       e.Cell.Selected = false;
}

Is there an extra workaround to override the cell behaviour for checkboxes?

A: 

I've found a workaround by adding the following code to the CellStateChanged event:

if (e.Cell is DataGridViewCheckBoxCell)
      e.Cell.Style.BackColor = BackgroundColor(e.Cell.RowIndex);

(BackgroundColor() calculates the cell background colour based on the row.)

This cures the problem, but could cause performance issues for larger or virtual tables, by causing creation of extra style objects.

Adrian Cox