2013-06-04 73 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