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

翻译|其它|编辑:郝浩|2007-09-30 09:48:31.000|阅读 3389 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

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


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:CSDN

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP