如何在asp.net中更改gridview单元格的颜色

问题描述 投票:0回答:1

我有gridview,它显示数据

Name  marks old     marks new    salaryold  salarynew
sasi     55         78            23000      45000
kiran    67         45            35000      20000
peter    23         23            10000      10000

我想要的是,如果旧值不同于新值,我想给出不同的颜色,将旧值的颜色设为绿色,将新值设为红色,如果没有变化则给予绿色。如何在Rowdatabound或任何其他活动中做到这一点?

c# asp.net .net gridview
1个回答
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");
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.