到面前为止,net里的DataGridView控件不能实现你的要求,如下:
private void dataGridView1_CellPainting( object sender, DataGridViewCellPaintingEventArgs e) ... { if (e.ColumnIndex == 2 && e.RowIndex != -1) //判断所在列 ...{ DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; int a = Convert.ToInt32(cell.Value); if (a > 10) //判断变色条件 ...{ //这里把整行的背景色都变了。 dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.BurlyWood; } } }//想必上面的代码你很清楚,DataGridView控件无法实现你的需求,两个解决办法://1.自己扩展DataGridView控件,使之实现你的要求.//2.找第三方控件.这里我用过DevExpress的GridControl控件,功能非常强大.完全可以实现这个功能,它可以直接设置每个单元格的外观.
// using DevExpress.XtraGrid.Views.Grid; // 注册RowStyleChange事件 private void gridView1_RowStyle( object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) ... { GridView view = sender as GridView; if (e.RowHandle >= 0) ...{ string category = view.GetRowCellDisplayText(e.RowHandle, view.Columns["Category"]); if (category == "Beverages") ...{ e.Appearance.BackColor = Color.Salmon; e.Appearance.BackColor2 = Color.SeaShell; } } }