[英]How to hide CheckBox in DataGridView if value of cell is null
我有包含表格的DataGridView
Winform,其中包含bit
。 如果值是null
,則需要隱藏CheckBox
,如果值是true
或false
,則使其保持可見。
如果單元格的值為null
如何在DataGridView
隱藏CheckBox
?
處理CellPainting事件,如果cell的值為null
或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;
將繪畫設置為已處理,因此默認繪畫邏輯將不會運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.