I know the checkbox size can change like this.
checkBox1.Size = new Size(10, 10);
I want to change the checkbox size in DataGridview with DataGridViewCheckBoxColumn and I tried to inherit DatagridviewCheckboxCell,but ever found any way to do as same.
class DGCBC : DataGridViewCheckBoxColumn
{
public DGCBC()
{
this.CellTemplate = new DatagridviewCheckboxCustomCell();
}
class DatagridviewCheckboxCustomCell : DataGridViewCheckBoxCell
{
public int row_index { get; set; }
/// <summary>
/// constructor
/// </summary>
///
public DatagridviewCheckboxCustomCell()
{
}
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)
{
*//I tried many way in there,but it's not work*
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}
}
}
To draw system controls in the current style of your machine you should use one of the many handy methods of the ControlPaint class .
Here is an example to draw three Checkboxes
onto a Panel
:
private void panel1_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawCheckBox(e.Graphics, 11, 11, 22, 22, ButtonState.Checked);
ControlPaint.DrawCheckBox(e.Graphics, 11, 44, 33, 33, ButtonState.Checked);
ControlPaint.DrawCheckBox(e.Graphics, 11, 88, 44, 44, ButtonState.Checked);
}
Of course you need to adapt this in your CellPainting
event to use the size you want and the coordinates of your cell!
Here is a simple example that pretty much fills the cell with the CheckBox
:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 1 && e.RowIndex >= 0)
{
e.PaintBackground(e.CellBounds, true);
ControlPaint.DrawCheckBox(e.Graphics, e.CellBounds.X + 1, e.CellBounds.Y + 1,
e.CellBounds.Width - 2, e.CellBounds.Height - 2,
(bool) e.FormattedValue ? ButtonState.Checked : ButtonState.Normal);
e.Handled = true;
}
You will want to find a size that suits your needs..
Note that you can combine some ButtonState . So to achieve a flat appearance, which is the default for DataGridView CheckBoxCells you can write ButtonState.Checked | ButtonState.Flat
ButtonState.Checked | ButtonState.Flat
etc..:
ControlPaint.DrawCheckBox(e.Graphics, 11, 11, 22, 22, ButtonState.Checked);
ControlPaint.DrawCheckBox(e.Graphics, 11, 44, 33, 33, ButtonState.Checked | ButtonState.Flat);
ControlPaint.DrawCheckBox(e.Graphics, 11, 88, 44, 44, ButtonState.Checked | ButtonState.Flat | ButtonState.Inactive);
To add onto @TaW's answer, this solution paints any checkbox column on dataGridView1
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)
{
e.PaintBackground(e.CellBounds, true);
ControlPaint.DrawCheckBox(e.Graphics, e.CellBounds.X + 1, e.CellBounds.Y + 1,
e.CellBounds.Width - 2, e.CellBounds.Height - 2,
(bool) e.FormattedValue ? ButtonState.Checked : ButtonState.Normal);
e.Handled = true;
}
}
}
In VB.NET this is:
Private Sub dataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dataGridView1.CellPainting
If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
Dim dgv As DataGridView = sender
If TypeOf dgv.Columns(e.ColumnIndex) Is DataGridViewCheckBoxColumn Then
e.PaintBackground(e.CellBounds, True)
ControlPaint.DrawCheckBox(e.Graphics, e.CellBounds.X + 1,
e.CellBounds.Y + 1, e.CellBounds.Width - 2, e.CellBounds.Height - 2,
If(CBool(e.FormattedValue), ButtonState.Checked, ButtonState.Normal))
e.Handled = True
End If
End If
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.