2013-04-04 44 views
0

我正在尋找修改我的gridview的狀態列中的每個結果的顏色,有無論如何做到這一點在我的查詢使用case語句或將我必須改變這一點?添加樣式到在sql查詢中的case語句中設置的數據

基本上,如果它正在等待它的紅色,如果它在審查其橙色,如果它完成其綠色。使用當前代碼完成此操作的最佳方法是什麼?

我正在做C#中的Visual Studio的一切。

我希望它看起來像這樣:

enter image description here

代碼背後的SQL語句,我聲明的狀態變量:

SqlCommand mySqlCommand = new SqlCommand(@"SELECT W.ID, W.Name, 
       CASE W.Type 
        WHEN 1 then 'U.S.' 
        WHEN 2 then 'Foreign' 
       end as Type, 
       CASE W.Status 
        WHEN 0 then 'Pending' 
        WHEN 1 then 'In Review' 
        WHEN 2 then 'Complete' 
       end as Status, 

...等

前:

<asp:GridView runat="server" ID="GridView1" CssClass="gridview" BorderColor="#E8CC6B" 
     BorderStyle="Solid" BorderWidth="1px" Font-Size="Medium" HorizontalAlign="Left" 
     AlternatingRowStyle-CssClass="even" Width="750px" AutoGenerateColumns="False" 
      AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging" 
      PageSize="25" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound"> <PagerStyle CssClass="cssPager" /><FooterStyle CssClass="cssFooter" /> 
     <AlternatingRowStyle CssClass="even"></AlternatingRowStyle> 
     <Columns> 
      <asp:BoundField DataField="ID" HeaderText="ID" /> 
      <asp:BoundField DataField="Name" HeaderText="Name" /> 
      <asp:BoundField DataField="Type" HeaderText="Type" /> 
      <asp:BoundField DataField="Status" HeaderText="Status" /> 
      </Columns> 
    </asp:GridView> 

回答

2

您可以使用GridView.RowDataBound事件來實現此目的。基於文本設置單元格顏色。

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e) 
    { 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     switch (e.Row.Cells[3].Text) 
     { 
      case "Panding": 
       e.Row.Cells[3].ForeColor = System.Drawing.Color.Red; 
       break; 
      case "Complete": 
       e.Row.Cells[3].ForeColor = System.Drawing.Color.Green; 
       break; 
      case "In Review": 
       e.Row.Cells[3].ForeColor = System.Drawing.Color.Yellow; 
       break; 
      default: 
       e.Row.Cells[3].ForeColor = System.Drawing.Color.Black; 
       break; 
     }; 

    } 
    } 
+0

你我的朋友是天賜之寶!這是完美的,效果很好。感謝您快速回復! – techora 2013-04-04 19:52:22