1

在 asp.net 应用程序中,我使用网格视图控件,因为我将数据绑定labelgrid-view.
如果数据为空,则该行的颜色应为红色。
如果不是,我的意思是如果有数据要绑定,则该行为绿色。这是我的代码:

<asp:TemplateField HeaderText ="Holiday Region">
     <ItemTemplate >
         <asp:Label ID ="lblholdareg" runat ="server" Text ='<%# Eval("Holidaregion") %>' >
         </asp:Label>
     </ItemTemplate>
</asp:TemplateField>
4

5 回答 5

2

您可以按以下rowdatabound功能执行此操作gridview

protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
         //change it according your cell number or find element
         if(e.Row.Cells[0].Text != "")
            e.Row.BackColor = Color.Green;
         else
            e.Row.BackColor = Color.Red;   
    }
}
于 2013-02-26T05:51:39.613 回答
2

您需要处理 RowDataBound 事件,进入 e.Row 项目,并分配 CSS 类或直接设置背景颜色。我更喜欢设置一个 CSS 类,这样您就可以在以后更改它的呈现而无需重新编译。

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Holiday Region">
            <ItemTemplate>
                <asp:Label ID="lblholdareg" runat="server" Text='<%# Eval("Holidaregion") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

在代码隐藏中,我不得不假设您使用 DataTable 作为数据源,更新代码以适合您的数据结构:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    System.Data.DataRow row = (System.Data.DataRow)e.Row.DataItem;
    if (row["Holidaregion"] == null || row["Holidaregion"].ToString().Trim().Length == 0)
    {
        e.Row.CssClass = "row-empty";
    }
    else 
    {
        e.Row.CssClass = "row-full";
    }
}
于 2013-02-26T08:31:46.857 回答
0

试试这个代码它是每行的颜色变化与类别明智或过滤明智 http://devloper4u.blogspot.in/2013/10/how-to-set-color-in-every-row-on.html

于 2013-12-21T18:00:30.370 回答
0

尝试这样的事情

<asp:TemplateField HeaderText ="Holiday Region">
  <ItemTemplate >
    <asp:Label ID ="lblholdareg" runat ="server"
     CSSClass='<%# (String.IsNullOrEmply(Eval("Holidaregion")))?"red:green" %>' 
     Text ='<%# Eval("Holidaregion") %>' >  
    </asp:Label>

   </ItemTemplate>
</asp:TemplateField>

编辑:

无需与网格视图内联和代码背后的东西打架,只需使用 jQuery 并在客户端实现相同的效果

于 2013-02-26T05:51:41.757 回答
0
if(e.Row.RowType == DataControlRowType.DataRow)
{
    Control l = e.Row.FindControl("Label1");
  ((Label)l).BackColor = System.Drawing.Color.Red;
}
于 2013-02-26T05:53:26.970 回答