2

I have the following DataGridViewCheckBoxCell. the problem is the visual update doesn't take place immediately in the edit mode, only when i quit it

public class CustomDataGridViewCell : DataGridViewCheckBoxCell
{
    protected override void Paint(Graphics graphics,
                                Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
                                DataGridViewElementStates elementState, object value,
                                object formattedValue, string errorText,
                                DataGridViewCellStyle cellStyle,
                                DataGridViewAdvancedBorderStyle advancedBorderStyle,
                                DataGridViewPaintParts paintParts)
    {
        base.Paint(graphics, clipBounds, cellBounds, rowIndex,
            elementState, value, formattedValue, errorText, cellStyle,
            advancedBorderStyle, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);

        var val = (bool?)FormattedValue;
        var img = val.HasValue && val.Value ? Properties.Resources._checked : Properties.Resources._unchecked;
        var w = img.Width;
        var h = img.Height;
        var x = cellBounds.Left + (cellBounds.Width - w) / 2;
        var y = cellBounds.Top + (cellBounds.Height - h) / 2;
        graphics.DrawImage(img, new Rectangle(x, y, w, h));
    }
}
ihisham
  • 248
  • 1
  • 10

1 Answers1

1

You need 2 fixes:

  1. You should create a CustomDataGridViewCheckBoxColumn as well which its cell template is set to your CustomDataGridViewCheckBoxCell.

  2. Instead of FormattedValue property, use formattedValue parameter.

Here is the code:

public class CustomDataGridViewCheckBoxColumn: DataGridViewCheckBoxColumn
{
    public CustomDataGridViewColumn()
    {
        this.CellTemplate = new CustomDataGridViewCheckBoxCell();
    }
}
public class CustomDataGridViewCheckBoxCell: DataGridViewCheckBoxCell
{
    protected override void Paint(Graphics graphics,
                                Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
                                DataGridViewElementStates elementState, object value,
                                object formattedValue, string errorText,
                                DataGridViewCellStyle cellStyle,
                                DataGridViewAdvancedBorderStyle advancedBorderStyle,
                                DataGridViewPaintParts paintParts)
    {
        base.Paint(graphics, clipBounds, cellBounds, rowIndex,
            elementState, value, formattedValue, errorText, cellStyle,
            advancedBorderStyle, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);
        var val = (bool?)formattedValue;
        var img = val.HasValue && val.Value ? Properties.Resources.Checked : Properties.Resources.UnChecked;
        var w = img.Width;
        var h = img.Height;
        var x = cellBounds.Left + (cellBounds.Width - w) / 2;
        var y = cellBounds.Top + (cellBounds.Height - h) / 2;
        graphics.DrawImage(img, new Rectangle(x, y, w, h));
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398