一套基于行/列值的GridView值ItemStyle前景色 [英] Set GridView Value ItemStyle ForeColor based on Row/Column Value

查看:123
本文介绍了一套基于行/列值的GridView值ItemStyle前景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.net的 GridView控件的该吐出三个数据列:(订单编号,OrderStatus和订购日期的)

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

我想设置的 OrderStatus字段值=红色如果状态=已取消

我不知道如何看待该字段的值输出的每一行并确定状态为已取消......那么,如果它被设置颜色为红色。

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.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#数据绑定:

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;
    }

}

任何建议将是极大的AP preciated。

Any suggestions would be greatly appreciated.

感谢一如既往,

推荐答案

您可以设置在的RowDataBound 事件OrderStatus列的前景色。您可以指定一个表格单元格的指数,你的情况OrderStatus将有一个索引1。

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;
        }
    }
}

这篇关于一套基于行/列值的GridView值ItemStyle前景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆