2010-05-08 30 views
1

我在網格視圖中顯示學生出勤。不同顏色的gridview數據顯示取決於文本

我選擇不存在作爲A,呈現爲P並且留下爲L.現在我想要以紅色顯示A,以綠色顯示P.

這是怎麼回事。請幫我

+2

所有你的問題是非常糟糕寫的,你沒有接受任何答案。 – Aristos 2010-05-08 07:30:15

回答

2

請試試這個,讓我知道了什麼問題,您面臨

protected void grdStudent_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     DataRow dr = ((DataRowView)e.Row.DataItem).Row; 
     if (dr["Present"].ToString() == "A") 
     { 
      ((Label)e.Row.FindControl("yourLableID")).ForeColor= System.Drawing.Color.Red; 
     //yourLableID is that lable in which you are showing A or P 
     } 
    } 
} 
2

我喜歡的方式是設置標記的顏色,

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> 
     <Columns> 
     <asp:TemplateField> 
     <ItemTemplate> 
      <asp:Label runat="server" ID="lblStatus" 
       Text='<%# Eval("Status") %>' 
       ForeColor='<%# GetItemColor(Eval("Status")) %>' /> 
     </ItemTemplate> 
     </asp:TemplateField> 
     </Columns> 
     </asp:GridView> 

基於代碼的方法後面

protected System.Drawing.Color GetForeColor(object statusObj) 
{ 
    System.Drawing.Color color = System.Drawing.Color.Black; 

    switch ((string)statusObj) 
    { 
     case "A": color = System.Drawing.Color.Red; break; 
     case "P": color = System.Drawing.Color.Green; break; 
     case "L": color = System.Drawing.Color.Yellow; break; 
    } 

    return color; 
} 

另外,你可以把邏輯直接放在標記中,但我更喜歡保持儘可能多的.cs文件中的C#代碼。
此外,asp:TemplateField爲您提供比asp:BoundField更大的靈活性。

你也可以設置背景色屬性更好的可視性,但我最喜歡的就是增加一個小的asp:圖像,其中ImageUrl屬性被以同樣的方式切換,表示狀態3個圖像之間。