改DatagridviewCheckboxCell中的复选框大小

改DatagridviewCheckboxCell中的复选框大小

本文介绍了如何更改DatagridviewCheckboxCell中的复选框大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道复选框的大小可以这样更改.

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

我想使用DataGridViewCheckBoxColumn更改DataGridview中的复选框大小,并尝试继承DatagridviewCheckboxCell,但是没有找到任何相同的方法.

  class DGCBC:DataGridViewCheckBoxColumn{公开DGCBC(){this.CellTemplate = new DatagridviewCheckboxCustomCell();}类DatagridviewCheckboxCustomCell:DataGridViewCheckBoxCell{public int row_index {get;放;}///< summary>///构造函数///</summary>///公共DatagridviewCheckboxCustomCell(){}受保护的重写void Paint(Graphics graphics,Rectangle clipBounds,Rectangle cellBounds,int rowIndex,DataGridViewElementStates elementState,对象值,对象formattedValue,字符串errorText,DataGridViewCellStyle cellStyle,DataGridViewAdvancedBorderStyle advancedBorderStyle,DataGridViewPaintParts paintParts){*//我尝试了很多方法,但是没有用*base.Paint(graphics,clipBounds,cellBounds,rowIndex,elementState,value,formattedValue,errorText,cellStyle,advancedBorderStyle,paintParts);}}} 
解决方案

要以当前计算机样式绘制系统控件,应使用

  private void panel1_Paint(对象发送者,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 事件中对此进行调整,以使用所需的大小和单元格的坐标!

这是一个简单的示例,几乎用 CheckBox 填充该单元格:

  private void dataGridView1_CellPainting(对象发送者,DataGridViewCellPaintingEventArgs e){如果(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,(布尔)e.FormattedValue吗?ButtonState.Checked:ButtonState.Normal);e.Handled = true;} 

您将希望找到适合您需求的尺寸..

请注意,您可以结合使用

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

这篇关于如何更改DatagridviewCheckboxCell中的复选框大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 10:10