根据行/列值设置GridView Value ItemStyle ForeColor

时间:2009-11-24 23:05:43

标签: c# asp.net

我有一个ASP.net GridView ,它会吐出三列数据:( OrderNumber,OrderStatus和OrderDate )。

我想设置OrderStatus Field Value = RED IF the status = "Cancelled"

我不确定如何查看输出的每一行的该字段的值,并确定状态是否为Canceled ...然后将颜色设置为RED。

ASP.net GridView:

<asp:GridView ID="gvOrders" 
                     runat="server" 
                     AutoGenerateColumns="False"
                     GridLines="None"  
                     AllowPaging="true" 
                     CssClass="mGrid"  
                     PagerStyle-CssClass="pgr"  
                     AlternatingRowStyle-CssClass="alt" > 
           <Columns>
             <asp:BoundField DataField="OrderNumber" HeaderText="OrderNumber" SortExpression="OrderNumber" />
             <asp:BoundField DataField="OrderStatus" HeaderText="OrderStatus" SortExpression="OrderStatus" />
             <asp:BoundField DataField="OrderDate" HeaderText="OrderDate"  SortExpression="OrderDate" />
           </Columns>
       </asp:GridView>

C#DataBinding:

protected void navOrders_Onclick(object sender, BulletedListEventArgs e)
{
    switch(e.Index)
    {
        case 0: //Orders
            DataTable dt = Procedures.GetOrderData();

            gvOrders.DataSource = dt;
            gvOrders.DataBind();

            break;
    }

}

任何建议都将不胜感激。

一如既往地谢谢,

1 个答案:

答案 0 :(得分:3)

您可以在RowDataBound事件中设置OrderStatus列的前景色。您可以指定表格单元格的索引,在您的情况下,OrderStatus的索引为1。

protected void gvOrders_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[1].Text == "Cancelled")
        {
            e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;
        }
    }
}