如果单元格值为空,如何隐藏 DataGridView 中的复选框

我有 Winform,DataGridView其中包含包含bit列的表。CheckBox如果值为 ,我需要隐藏null;如果值为true或 ,则需要保持其可见false

如果单元格的值为 ,我如何CheckBox隐藏?DataGridViewnull


千万里不及你
浏览 105回答 1
1回答

三国纷争

处理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;将绘画设置为已处理,因此默认的绘画逻辑将不会运行。它不会使单元格变为只读。它只是跳过渲染复选框。不要忘记将事件处理程序添加到事件中。
打开App,查看更多内容
随时随地看视频慕课网APP