根据asp.net c中的条件更改gridview单元格的前景色

时间:2018-01-11 12:38:54

标签: c# asp.net gridview

如果值为<=0,我需要将gridview单元格颜色更改为红色。我已经尝试了下面的代码,它没有采用每一行的单元格值。

如何获取gridview的每个单元格值。

代码:

 if (e.Row.RowType == DataControlRowType.DataRow)
            {
                foreach (GridViewRow row in GridView1.Rows)
                {
                    for (int i = 0; i < GridView1.Columns.Count; i++)
                    {
                        TableCell cell = row.Cells[i];
                        int quantity = int.Parse(cell.Text);
                        if (quantity <= 0)
                        {
                            cell.ForeColor = Color.Red;
                        }
                    }
                }
            }

3 个答案:

答案 0 :(得分:0)

如果要更改前景色或访问文本,则应使用itemtemplate而不是boundField。

<asp:TemplateField HeaderText="Status" ControlStyle-Width="75px" >
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("ParentID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

你可以绑定这个变量,如

 protected void GridView1_DataBound(object sender, EventArgs e)
{
    for (int i =0 ; i <= GridView1.Rows.Count -1 ;i++)
    {
        Label lblparent = (Label)GridView1.Rows[i].FindControl("Label1");

        if (lblparent.Text == "1")
        {
            GridView1.Rows[i].Cells[6].BackColor = Color.Yellow;
            lblparent.ForeColor = Color.Black;
        }
        else
        {
            GridView1.Rows[i].Cells[6].BackColor = Color.Green;
            lblparent.ForeColor = Color.Yellow;
        }

    }
}

获取更多帮助,请查看此内容 https://forums.asp.net/t/1747819.aspx?how+to+change+gridview+cell+color+based+on+cell+item

答案 1 :(得分:0)

这是您要查找的代码:

    Label lblInfo = (Label)gvVisa_Mofa.Rows[i].FindControl("lblInfo");

    if (lblInfo.Text <= "0")
    {
        gvVisa_Mofa.Rows[i].BackColor = System.Drawing.ColorTranslator.FromHtml("#EDA3A3");

    }

另一种方法:

if (e.Row.RowType == DataControlRowType.DataRow)
{

    If(Condition True)
    {
        e.Row.BackColor = Drawing.Color.Red 
    }

}

答案 2 :(得分:0)

您的代码对我有用。我将它放入DataBound事件:

    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            for (int i = 0; i < GridView1.Columns.Count; i++)
            {
                TableCell cell = row.Cells[i];
                int quantity = int.Parse(cell.Text);
                if (quantity <= 0)
                {
                    cell.ForeColor = System.Drawing.Color.Red;
                }
            }
        }
    }

我的gridview代码是:

<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false" OnDataBound="GridView1_DataBound">
<Columns>
    <asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-Width="30" />
    <asp:BoundField DataField="quantity" HeaderText="Quantity" ItemStyle-Width="150" />
    <asp:BoundField DataField="number" HeaderText="number" ItemStyle-Width="150" />
</Columns>
</asp:GridView>

我认为你的GridView1上的关键是OnDataBound =“GridView1_DataBound”

相关问题