好像是dataGrid.DisplayStyle.BackColor或者是dataGrid.Rows[i].DisplayStyle.BackColor,自己试试吧!

解决方案 »

  1.   

    刚写了一篇这方面的文章,不过是datagird的,不是dataGridview,2005支持datagrid,有兴趣你可以看看。
    http://blog.csdn.net/tjvictor/archive/2007/01/22/1489972.aspx
      

  2.   

    我想知道如何只把单元格的颜色变化。当一个DataGridView的第一行第三列的数据为"异常"时,此是只把第一行第三列的单元格的颜色变为红色,怎么实现呢?这个例子好实现吗?大家帮帮忙。
      

  3.   

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if(e.RowIndex == 1 && e.ColumnIndex == 0)
                {
                    e.CellStyle.BackColor = Color.Red;
                }
            }
      

  4.   

    lz:到面前为止,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;
    // ...
    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;
          }            
       }
    }
      

  5.   

    不好意思,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)
                    {
                        e.CellStyle.BackColor = Color.BurlyWood;
                        //dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.BurlyWood;
                    }            }
            }平时总是考虑DataGridViewCell ,把事件参数的信息给忽略了。
      

  6.   

    现在的DataGridView不比GridControl差了,而且扩展功能强
    以前2003也是用DataGrid扩展
      

  7.   

    现在让单元格的颜色变化,我实现了。这段代码我放在form_load里面。
                for (int i = 0; i < dataGridView2.Rows.Count; i++)
                {
                    if (dataTable2.Rows[i].ItemArray[4].ToString().Equals("警告"))
                    {
                        dataGridView2.Rows[i].Cells[4].Style.BackColor = Color.Red;
                    }
                    else if (dataTable2.Rows[i].ItemArray[4].ToString().Equals("異常"))
                    {
                        dataGridView2.Rows[i].Cells[4].Style.BackColor = Color.Yellow;
                    }
                    else if (dataTable2.Rows[i].ItemArray[4].ToString().Equals("正常"))
                    {
                        dataGridView2.Rows[i].Cells[4].Style.BackColor = Color.Green;
                    }
                }但是为什么我点击列头排序时颜色不随着上面的判断条件发生变化呢(上面的代码我放在dataGridView2_CellPainting),难道单元格的颜色第一次改了以后就不能改了吗?
      

  8.   

    sdl2005lyx() 像是在做广告,呵呵!