2013-06-04 33 views
0

我有一个有条件获取基于单元格格式的gridview。它在页面加载时效果很好,但是当我选择一行时,它会为所有行的第一个给定条件(黑色)进行格式化。这是条件格式的代码。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

您正在使用哪种方法?数据绑定?的DataBind? RowCreating?你能发布这个吗? – Fals

+0

也假设你已经启用了行选择,选中的行有自己的一组格式,需要重写 - 你需要在行选择事件中制定相同的条件格式 – fnostro

+0

这是RowCreated方法。 – cal5barton

回答

0

作为@fnostro建议,我将方法从RowCreated更改为RowDataBound,它完美地工作!

+0

谢谢!很高兴它对你有效 – fnostro