Gridview的条件格式在行选择后不工作
本文关键字:选择 工作 行选 条件 格式 Gridview | 更新日期: 2023-09-27 18:16:50
我有一个gridview,根据单元格值有条件地格式化。当页面加载时,它工作得很好,但是当我选择一行时,它为所有行的第一个给定条件(黑色)格式化它。下面是条件格式的代码。
//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;
}
}
}
任何可能导致问题或如何解决问题的帮助将是伟大的。
我改变了方法从RowCreated到RowDataBound作为@fnostro建议,它工作完美!