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 關注
- 144 瀏覽
添加回答
舉報