关于Grid控件中设置行,单元格颜色

我的CSDN博客转移过来,发布日期:2007-09-30

到面前为止,net里的DataGridView控件不能实现你的要求,如下:

Code Snippet
  1.     private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  2.     {
  3.         if (e.ColumnIndex == 2 && e.RowIndex != -1)  //判断所在列
  4.         {
  5.             DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
  6.             int a = Convert.ToInt32(cell.Value);
  7.             if (a > 10)  //判断变\u-32142 ?条件
  8.             {
  9.                 //\u-28711 ?\u-28212 ?把整\u-30644 ?的\u-32564 ?景\u-32142 ?\u-28419 ?变了。
  10.                 dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.BurlyWood;
  11.             }
  12.         }
  13.     \


想必上面的代码你很清楚,DataGridView控件无法实现你的需求,两个解决办法:
1.自己扩展DataGridView控件,使之实现你的要求.
2.找第三方控件.这里我用过DevExpress的GridControl控件,功能非常强大.完全可以实现这个功能,它可以直接设置每个单元格的外观.

Code Snippet
  1.     using DevExpress.XtraGrid.Views.Grid;
  2.     //注册RowStyleChange事件
  3.     private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
  4.     {
  5.         GridView view = sender as GridView;
  6.         if (e.RowHandle >= 0)
  7.         {
  8.             string category = view.GetRowCellDisplayText(e.RowHandle, view.Columns["Category"]);
  9.             if (category == "Beverages")
  10.             {
  11.                 e.Appearance.BackColor = Color.Salmon;
  12.                 e.Appearance.BackColor2 = Color.SeaShell;
  13.             }
  14.         }
  15.     \
posted @ 2009-08-09 10:12 Tony Chi 阅读(...) 评论(...) 编辑 收藏