0

I have a gridview that conditionally get formatted based on a cell value. It works great when the page loads, however when I select a row it formats it for the first given condition (black) for all of the rows. Here is the code for the conditional formatting.

 //Conditionally formats the gridview to show banner colors
protected void EmployeeAchievementsGV_RowCreated(object sender, GridViewRowEventArgs e)
{
    //Black Banner
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        int CellValue = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "TotalAchievementsEarned"));
        if (CellValue >= 0 && CellValue < 12)
        {
            e.Row.BackColor = Color.Black;
            e.Row.Cells[0].ForeColor = Color.White;
            e.Row.Cells[1].ForeColor = Color.White;
            e.Row.Cells[2].ForeColor = Color.White;
        }
    }

    //Yellow Banner
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int CellValue = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "TotalAchievementsEarned"));
        if (CellValue >= 12 && CellValue < 24)
        {
            e.Row.BackColor = Color.Yellow;
            e.Row.Cells[0].ForeColor = Color.Black;
            e.Row.Cells[1].ForeColor = Color.Black;
            e.Row.Cells[2].ForeColor = Color.Black;
        }
    }

    //Blue Banner
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int CellValue = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "TotalAchievementsEarned"));
        if (CellValue >= 24 && CellValue < 36)
        {
            e.Row.BackColor = Color.DodgerBlue;
            e.Row.Cells[0].ForeColor = Color.White;
            e.Row.Cells[1].ForeColor = Color.White;
            e.Row.Cells[2].ForeColor = Color.White;
        }
    }

    //Orange Banner
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int CellValue = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "TotalAchievementsEarned"));
        if (CellValue >= 36 && CellValue < 48)
        {
            e.Row.BackColor = Color.Orange;
            e.Row.Cells[0].ForeColor = Color.Black;
            e.Row.Cells[1].ForeColor = Color.Black;
            e.Row.Cells[2].ForeColor = Color.Black;
        }
    }

    //Pink Banner
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int CellValue = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "TotalAchievementsEarned"));
        if (CellValue >= 48)
        {
            e.Row.BackColor = Color.HotPink;
            e.Row.Cells[0].ForeColor = Color.Black;
            e.Row.Cells[1].ForeColor = Color.Black;
            e.Row.Cells[2].ForeColor = Color.Black;
        }
    }
}

Any help on what might be causing the issue or how to resolve the issue would be great.

4

1 回答 1

0

我按照@fnostro 的建议将方法从 RowCreated 更改为 RowDataBound 并且效果很好!

于 2013-06-05T19:19:30.103 回答