我有一个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;
}
}
}
对可能导致问题或如何解决问题的任何帮助都会很棒。
答案 0 :(得分:0)
我将方法从RowCreated更改为RowDataBound,因为@fnostro建议它完美无缺!