1 回答

TA貢獻(xiàn)1804條經(jīng)驗 獲得超7個贊
處理CellPaintingnull
事件,如果單元格的值為或,則不繪制復(fù)選框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
確保我們渲染的是數(shù)據(jù)單元格,而不是標(biāo)題單元格。e.ColumnIndex == 1
確保我們正在對索引 1 處的列應(yīng)用邏輯。如果您想要另一列的邏輯,請使用 ?hat 列的索引。e.Paint(...);
正在繪制單元格的所有部分,除了作為復(fù)選框的單元格內(nèi)容前景。e.Handled = true;
將繪畫設(shè)置為已處理,因此默認(rèn)的繪畫邏輯將不會運(yùn)行。它不會使單元格變?yōu)橹蛔x。它只是跳過渲染復(fù)選框。
不要忘記將事件處理程序添加到事件中。
- 1 回答
- 0 關(guān)注
- 141 瀏覽
添加回答
舉報