lundi 6 juillet 2020

Drawn DataGridView CheckBox only registers clicks on the right hand side of box

I'm creating a Windows forms application in C#. I have a DataGridView with CheckBox columns.

To increase the size of the CheckBoxes, I've put some code within the CellPainting event for the DataGridView. This successfully changes the size but I have found that for some reason, the left side of the CheckBox cannot be clicked to change the Checked status of the CheckBox.

Does anyone know why this might be happening? And how can I fix it so that the whole box can be clicked.

Thanks

Cell painting:

private void Tasks_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            try
            {               
                DataGridView dgv = (DataGridView)sender;

                dgv.RowTemplate.Height = 30;

                if (dgv.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)
                {
                    e.PaintBackground(e.CellBounds, true);
                    ControlPaint.DrawCheckBox(e.Graphics, e.CellBounds.X + 1, e.CellBounds.Y + 1,
                        25, e.CellBounds.Height - 2,
                        (bool)e.FormattedValue ? ButtonState.Checked : ButtonState.Normal);
                    e.Handled = true;                   
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }



Aucun commentaire:

Enregistrer un commentaire