开发者

Set GridView Value ItemStyle ForeColor based on Row/Column Value

开发者 https://www.devze.com 2022-12-12 05:08 出处:网络
I have an ASP.net GridView that spits out three columns of data: (OrderNumber, OrderStatus, and OrderDate).

I have an ASP.net GridView that spits out three columns of data: (OrderNumber, OrderStatus, and OrderDate).

I want to set the OrderStatus Field Value = RED IF the status = "Cancelled"

I am not sure how to look at the value of that field for each row of the output and determine if the status is Cancelled...then if it is set the color to RED.

ASP.net GridView:

<asp:GridView ID="gvOrders" 
                     runat="server" 
                     AutoGenerateColumns="False"
                     GridLines="None"  
                     AllowPaging="true" 
                     CssClass="mGrid"  
                     PagerStyle-CssClass="pgr"  
                     AlternatingRowStyle-Css开发者_运维问答Class="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;
    }

}

Any suggestions would be greatly appreciated.

Thanks as always,


You can set the fore color of the OrderStatus column in the RowDataBound event. You can specify the index of a table cell, in your case the OrderStatus would have an index of 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;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消