2013-04-04 41 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