我知道复选框大小可以像这样改变。
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);
}
}
}
发布于 2016-03-23 16:10:47
要以计算机的当前样式绘制系统控件,您应该使用ControlPaint class的许多方便方法中的一个。
下面是一个在Panel
上绘制三个Checkboxes
的示例
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
填充单元格
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);
发布于 2020-04-07 08:55:48
要添加到@TaW's answer上,此解决方案将在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
https://stackoverflow.com/questions/36171250
复制相似问题