C# .NET 如何在DataGridView中最簡潔的實現對單元格顯示的處理
//如何在DataGridView中最簡潔的實現對單元格顯示的處理
//方法
private void dataGridView1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
//加入對單元格數據的判斷
//例如: //可以對e.各個屬性值進行處理判斷
if (e.ColumnIndex == dataGridView1.Columns["列名"].Index && e.Value != null)
{
//處理符合if條件的單元格的顯示樣式
e.CellStyle.SelectionForeColor = Color.Red; e.CellStyle.ForeColor = Color.Red; e.CellStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
}
}
//在需要觸發的時間中加入調用dataGridView1_CellFormatting方法,重載dataGridView的單元格顯示樣式
dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
//OK,這樣實現了,最簡潔的方法,不用通過遍歷整個dataTable來實現了。
//效果例圖
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.