我有 Winform,DataGridView其中包含包含bit列的表。CheckBox如果值为 ,我需要隐藏null;如果值为true或 ,则需要保持其可见false。如果单元格的值为 ,我如何CheckBox隐藏?DataGridViewnull
1 回答
三国纷争
TA贡献1804条经验 获得超7个赞
处理CellPaintingnull
事件,如果单元格的值为或,则不绘制复选框DBNnull.Value
:
private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex == 1 &&
(e.Value == DBNull.Value || e.Value == null))
{
e.Paint(e.ClipBounds, DataGridViewPaintParts.All &
~DataGridViewPaintParts.ContentForeground);
e.Handled = true;
}
}
笔记:
e.RowIndex >= 0
确保我们渲染的是数据单元格,而不是标题单元格。e.ColumnIndex == 1
确保我们正在对索引 1 处的列应用逻辑。如果您想要另一列的逻辑,请使用 فhat 列的索引。e.Paint(...);
正在绘制单元格的所有部分,除了作为复选框的单元格内容前景。e.Handled = true;
将绘画设置为已处理,因此默认的绘画逻辑将不会运行。它不会使单元格变为只读。它只是跳过渲染复选框。
不要忘记将事件处理程序添加到事件中。
- 1 回答
- 0 关注
- 104 浏览
添加回答
举报
0/150
提交
取消