如何在DatagridviewCheckboxCell中更改checkBox大小

时间:2016-03-23 06:32:22

标签: c# .net

我知道复选框大小可以像这样改变。

checkBox1.Size = new Size(10, 10);

我想用DataGridViewCheckBoxColumn更改DataGridview中的复选框大小,我试图继承DatagridviewCheckboxCell,但是却找不到任何方法。

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);
        }

    }
}

2 个答案:

答案 0 :(得分:1)

要在机器的当前样式中绘制系统控件,您应该使用ControlPaint class中许多方便的方法之一。

以下是在Checkboxes上绘制三个Panel的示例:

enter image description here

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);
}

当然,您需要在CellPainting事件中对此进行调整,以使用您想要的尺寸和单元格的坐标!

这是一个简单的示例,几乎用<{1}}填充单元格:

enter image description here

CheckBox

您需要找到适合您需求的尺寸。

请注意,您可以合并一些ButtonState。因此,要实现平面外观,这是DataGridView CheckBoxCells的默认设置,您可以编写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; } 等..:

ButtonState.Checked | ButtonState.Flat

enter image description here

答案 1 :(得分:1)

要添加到Update Guide答案中,此解决方案会在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;
        }
    }
}

在VB.NET中,这是:

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