簡體   English   中英

如果單元格的值為null,如何在DataGridView中隱藏CheckBox

[英]How to hide CheckBox in DataGridView if value of cell is null

我有包含表格的DataGridView Winform,其中包含bit 如果值是null ,則需要隱藏CheckBox ,如果值是truefalse ,則使其保持可見。

如果單元格的值為null如何在DataGridView隱藏CheckBox

處理CellPainting事件,如果cell的值為nullDBNnull.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM