当前位置:编程学习 > C#/ASP.NET >>

关于DataGridView的颜色变化(着急)

请问我想让DataGridView的背景颜色变化。

举个例子:

当一个DataGridView的第一行第三列的数据为"异常"时,此是只把第一行第三列的单元格的颜色变为红色,怎么实现呢?

谢谢各位! --------------------编程问答-------------------- 好像是dataGrid.DisplayStyle.BackColor或者是dataGrid.Rows[i].DisplayStyle.BackColor,自己试试吧! --------------------编程问答-------------------- 刚写了一篇这方面的文章,不过是datagird的,不是dataGridview,2005支持datagrid,有兴趣你可以看看。
http://blog.csdn.net/tjvictor/archive/2007/01/22/1489972.aspx --------------------编程问答-------------------- 我想知道如何只把单元格的颜色变化。

当一个DataGridView的第一行第三列的数据为"异常"时,此是只把第一行第三列的单元格的颜色变为红色,怎么实现呢?这个例子好实现吗?

大家帮帮忙。 --------------------编程问答-------------------- private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if(e.RowIndex == 1 && e.ColumnIndex == 0)
            {
                e.CellStyle.BackColor = Color.Red;
            }
        } --------------------编程问答-------------------- 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;
      }            
   }
}
--------------------编程问答-------------------- 楼上的有意思 :) --------------------编程问答-------------------- 不好意思,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 ,把事件参数的信息给忽略了。。。。 --------------------编程问答-------------------- 现在的DataGridView不比GridControl差了,而且扩展功能强
以前2003也是用DataGrid扩展 --------------------编程问答-------------------- 谢谢ls的指教,还望以后有机会多指点。。。。 --------------------编程问答-------------------- 现在让单元格的颜色变化,我实现了。这段代码我放在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),难道单元格的颜色第一次改了以后就不能改了吗? --------------------编程问答-------------------- 看看!!! --------------------编程问答-------------------- 帮忙呀,不要看:) --------------------编程问答-------------------- 大家都不知道吗? --------------------编程问答-------------------- 搞定,结贴了。


谢谢大家帮忙! --------------------编程问答-------------------- sdl2005lyx() 

像是在做广告,呵呵!
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,