在GridView ASP.NET中设置DataRow的ForeColor(不限于DataSource)

时间:2017-02-04 11:26:01

标签: c# asp.net gridview colors cells

我更改了第一行单元格颜色,但我不希望第二行(Alloted Beds数量)受到影响。我可以知道我该怎么做?

protected void bedStatsClass_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TableCell cell = e.Row.Cells[1];

            int number = int.Parse(cell.Text);

            if (number <= 10)
            {
                cell.ForeColor = Color.Red;
                cell.Style.Add("font-weight", "bold");
            }
            else if (number > 10)
            {
                cell.ForeColor = Color.ForestGreen;
                cell.Style.Add("font-weight", "bold");
            }

            //So on and so forth..
        }
    }

图示说明

does not refer

感谢帮助。谢谢。

2 个答案:

答案 0 :(得分:2)

数据行的完整解决方案,即网格视图的标签,但不是数据源的数据绑定,我们可以使用没有OnRowDataBound的direclty。请尝试这个最佳解决方案。所以我们有一个例子

在aspx页面中:

<asp:GridView ID="GridViewBalance" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                            DataKeyNames="card_no"  GridLines="None" CssClass="table table-striped"
                            OnPageIndexChanging="GridViewBalance_PageIndexChanging" PageSize="50" AllowPaging="True"
                             CellSpacing="4">                           
                            <Columns>
                                <asp:BoundField DataField="card_no" HeaderText="Card No" />
                                <asp:BoundField DataField="acc_no" HeaderText="Account No" />
                                <asp:BoundField DataField="cname" HeaderText="Name" />                         
                                <asp:BoundField DataField="mobileno" HeaderText="Mobile No" />
                                <asp:BoundField DataField="balance" HeaderText="Balance" />
                                <asp:BoundField DataField="created_date" HeaderText="Creation date" DataFormatString="{0:d/M/yyyy HH:mm:ss}" />                            

                                <asp:TemplateField HeaderText="Active-Status">
                                    <ItemTemplate>
                                    <asp:Label ID="lblStatus" runat="server" ForeColor="White"  Text='<%# GetStatus(Eval("is_active").ToString()) %>' BackColor='<%# GetColor(Eval("is_active").ToString()) %>' ToolTip="Transaction Status"></asp:Label>

                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>

                        </asp:GridView> 

代码背后:

This is used for the color of the grid view data rows.

   public System.Drawing.Color GetColor(string butColor)
        {
            System.Drawing.Color cr = new System.Drawing.Color();
            if (butColor == "1")
            {
                cr = System.Drawing.Color.Green;
            }
            else if (butColor == "0")
            {
                cr = System.Drawing.Color.Red;
            }
            return cr;
        }

 This is used for the status:

   protected string GetStatus(string butStatus)
        {
            if (butStatus == "1")
            {
                butStatus = "ACTIVE";
            }
            else if (butStatus == "0")
            {
                butStatus = "DEACTIVE";
            }
            return butStatus;
        }

答案 1 :(得分:1)

您可以使用RowIndex检查行号。然后,您可以根据该数字为元素着色。

protected void bedStats_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.RowIndex == 0)
        {
            //do stuff with the first row
        }
        if (e.Row.RowIndex % 2 == 0)
        {
            //if you want to do something to rows 1, 3, 5 etc
        }
    }
}