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

如何设置datagrid 单元格颜色?

datagrid中有如下数据 
姓名     性别       年龄 
S             男         20 
A             女         22 
B             女         30 

要求将年龄大于等30的格子以黄色背景显示,性别为男的数据行以蓝色字体显示,请问应该怎么做啊? 
谢谢了
【答】:


//... 

//使用datagridtablestyle 显示datagrid. 

datagridtablestyle tablestyle = new datagridtablestyle(); 
tablestyle.mappingname = "customers"; 

int numcols = _dataset.tables["customers"].columns.count; 
datagridcellcolortextboxcolumn columntextcolumn ; 
for(int i = 0; i < numcols; ++i) 

columntextcolumn = new datagridcellcolortextboxcolumn(); 
columntextcolumn.headertext = _dataset.tables["customers"].columns[i].columnname; 
columntextcolumn.mappingname = _dataset.tables["customers"].columns[i].columnname; 

//为每个单元格建立设置背景色的事件. 
columntextcolumn.checkcellcolor += new cellcoloreventhandler(setcolorvalues); 

tablestyle.gridcolumnstyles.add(columntextcolumn); 


datagrid1.tablestyles.clear(); 
datagrid1.tablestyles.add(tablestyle); 

datagrid1.datasource = _dataset.tables["customers"]; 





public void setcolorvalues(object sender, datagridcellcoloreventargs e) 

//根据条件, 将相关行设置不同的背景色. 
//下例为国家(datagrid中第9列)为mexico的行设置为红色,usa的行设为黄色. 
if(convert.tostring(datagrid1[e.row,8]) == "mexico") 
e.backcolor = color.red; 
else if(convert.tostring(datagrid1[e.row,8]) == "usa") 
e.backcolor = color.yellow; 



public class datagridcellcoloreventargs : eventargs 

private int _row; 
private color _backcolor; 

public datagridcellcoloreventargs(int row, color val) 

_row = row; 
_backcolor = val; 

public int row 

get{ return _row;} 
set{ _row = value;} 

public color backcolor 

get{ return _backcolor;} 
set{ _backcolor = value;} 





//为事件建立委托. 
public delegate void cellcoloreventhandler(object sender, datagridcellcoloreventargs e); 

public class datagridcellcolortextboxcolumn : datagridtextboxcolumn 

public event cellcoloreventhandler checkcellcolor; 

public datagridcellcolortextboxcolumn() 



//继承datagridtextboxcolumn的pain事件. 
protected override void paint(system.drawing.graphics g, system.drawing.rectangle bounds, system.windows.forms.currencymanager source, int rownum, system.drawing.brush backbrush, system.drawing.brush forebrush, bool aligntoright) 

if(checkcellcolor != null) 

//重绘画时,设置当前行的背景色 
datagridcellcoloreventargs e = new datagridcellcoloreventargs(rownum, color.white); 
checkcellcolor(this, e); 

if(e.backcolor != color.white) 
backbrush = new solidbrush(e.backcolor); 


base.paint(g, bounds, source, rownum, backbrush, forebrush, aligntoright); 


protected override void edit(system.windows.forms.currencymanager source, int rownum, system.drawing.rectangle bounds, bool readonly, string instanttext, bool cellisvisible) 

base.edit(source, rownum, bounds, readonly, instanttext, cellisvisible); 

}  --------------------编程问答-------------------- 楼主自问自答? --------------------编程问答-------------------- 你是来问的,还是来交流的 --------------------编程问答-------------------- http://wenku.baidu.com/view/a10a052bbd64783e09122baf.html --------------------编程问答-------------------- 是来送分的? --------------------编程问答-------------------- 你自己又问又答

 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            DataRowView mydrv = myds.Tables["飞狐工作室"].DefaultView[i];
            string score = Convert.ToString(mydrv["起薪"]);
            if (Convert.ToDouble(score) < 34297.00)//大家这里根据具体情况设置可能ToInt32等等

            {
                GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;
            }
        }
--------------------编程问答--------------------             foreach(DataGridViewRow row in dataGridView1.Rows)
            {
                if (Convert.ToInt32(row.Cells[2].Value) >= 31)
                {
                    row.Cells[2].Style.BackColor = Color.Yellow;
                }
                if (Convert.ToString(row.Cells[1].Value)== "男")
                {
                  
                    Graphics g = this.dataGridView1.CreateGraphics();
                    Font myFont = new Font("宋体", 20);
                    SolidBrush brush = new SolidBrush(Color.Blue);
                    g.DrawString("男", myFont, brush, x, y);
                }
            } --------------------编程问答-------------------- 接分来了 --------------------编程问答-------------------- 看来LZ是送分来了 --------------------编程问答-------------------- 送分?? --------------------编程问答-------------------- LZ自己都答了,接分好了
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,