2016-03-23 77 views
1

我知道复选框的大小可以像这样改变。如何更改DatagridviewCheckboxCell中的复选框大小

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

    } 
} 
+0

你可以发布你试过的代码。 – Shanid

+0

对不起,因为我认为重写Paint看起来是错误的方向,所以没有发布。 – Jason

+0

检查了这一点:http://stackoverflow.com/questions/10117477/how-to-change-checkbox-size-in-datagridview-winform – princevezt

回答

1

要绘制系统控制在你的机器目前的风格,你应该使用的ControlPaint class的许多方便的方法之一。

下面是一个例子绘制三条CheckboxesPanel

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事件使用你想要的大小和坐标,以适应这个你细胞!

下面是一个简单的例子,几乎充满CheckBox细胞:

enter image description here

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。因此,为了实现扁平化的外观,这是DataGridView的CheckBoxCells默认你可以写ButtonState.Checked | ButtonState.Flat等:

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

enter image description here

+0

感谢您的帮助,它炒锅! – Jason

+0

如果您对答案感到满意,请考虑考虑[接受](http://stackoverflow.com/help/accepted-answer)它..! - 我发现你从来没有这样做过:在答案的选票下面,点击左上角的(不可见)复选标记,然后单击它!它变成了绿色,并且获得了我们的一点声誉.. – TaW