我有gridview,它显示的数据如
Name marks old marks new salaryold salarynew
sasi 55 78 23000 45000
kiran 67 45 35000 20000
peter 23 23 10000 10000
我想要的是,如果旧值不同于新值,我想给出不同的颜色,将旧值的颜色设为绿色,将新值设为红色,如果没有变化则给予绿色。如何在Rowdatabound
或任何其他事件中执行此操作?
答案 0 :(得分:1)
这应该这样做(抱歉,在这台机器上没有VS)。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Proper input validation should be done here. i.e. empty string etc
var oldSalary = Convert.ToInt32(e.Row.Cells[3].Text);
var newSalary = Convert.ToInt32(e.Row.Cells[4].Text);
if(oldSalary == newSalary){
e.Row.Cells[3].ForeColor = Color.FromName("green");
e.Row.Cells[4].ForeColor = Color.FromName("green");
}
else{
e.Row.Cells[3].ForeColor = Color.FromName("green");
e.Row.Cells[4].ForeColor = Color.FromName("red");
}
}
}
或
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var lblOldSalary = e.Row.FindControl("lblOldSalary") as Label;
var lblNewSalary = e.Row.FindControl("lblNewSalary") as Label;
//Proper input validation should be done here. i.e. empty string etc
var oldSalary = Convert.ToInt32(lblOldSalary.Text);
var newSalary = Convert.ToInt32(lblNewSalary.Text);
if(oldSalary == newSalary){
lblOldSalary.ForeColor = Color.FromName("green");
lblNewSalary.ForeColor = Color.FromName("green");
}
else{
lblOldSalary.ForeColor = Color.FromName("green");
lblNewSalary.ForeColor = Color.FromName("red");
}
}
}