[英]Conditional Formatting of Gridview Not Working After Row Selection
我有一個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;
}
}
}
任何可能導致該問題或如何解決該問題的幫助都將非常有用。
我按照@fnostro的建議將方法從RowCreated更改為RowDataBound,並且效果很好!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.