请教:在dataGrid 中如何根据行值的不同,设置不同的颜色

请教高手;(1)在dataGrid中,假如第三栏是age,根据的age不同,row的颜色也不同,如age>30,blue,age<=30,red
(2):DataGridBoolColumn 有三种状态,null,true,false,我想怎样能它只有true,和false;
谢谢
---------------------------------------------------------------

1 重载DataGridTextBoxColumn的Paint方法:
public class DataGridColorColumn:DataGridTextBoxColumn
{
protected override void Paint(Graphics g,Rectangle rc,CurrencyManager cm,int rowNumber,Brush backbrush,Brush forebrush,bool LtoR)
{
object o=this.GetColumnValueAtRow(cm,rowNumber);
if (o!=null)
{
if (o.ToString().SubString(0,1)<="30")
{
backbrush=new SolidBrush(Color.Red);
}
else
if (o.ToString().SubString(0,1)>"30")
{
backbrush=new SolidBrush(Color.Blue);
}

}
base.Paint(g,rc,cm,rowNumber,backbrush,forebrush,true);
}

在程序中使用这个类,示例:
private void Form1_Load(object sender, System.EventArgs e)
{
SqlDataAdapter da=new SqlDataAdapter("select age from employees",sqlConn);
DataSet ds=new DataSet();
da.Fill(ds,"employees");

DataGridTableStyle ats=new DataGridTableStyle();
ats.MappingName="employees";

DataGridColorColumn dcs=new DataGridColorColumn();
dcs1.MappingName="age";

ats.GridColumnStyles.Add(dcs);
this.dataGrid1.TableStyles.Add(ats);

this.dataGrid1.DataSource=ds;
this.dataGrid1.DataMember="employees";
}

2
DataGridBoolColumn dc;
dc.AllowNull=false;
---------------------------------------------------------------

绑定客户端事件。
服务器端cs文件
DataGrid1.Items[0].Attribute.Add("onmousemove","MouseMove(this,"+Age+")")
客户端html文件
function MouseMove(Element,Age)
{
if(Age>30)
{
Element.style.color="red"
}
}

Published At
Categories with Web编程
Tagged with
comments powered by Disqus