【问题标题】:Row background color in gridview?gridview中的行背景颜色?
【发布时间】:2013-02-11 11:56:31
【问题描述】:

在 asp.net 应用程序中,我使用网格视图控件,因为我将数据绑定到位于 grid-view 中的 label
如果数据为空,则行的颜色应为红色
如果不是,我的意思是如果有数据可以绑定,那么 绿色行。 这是我的代码:

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

【问题讨论】:

    标签: asp.net css gridview


    【解决方案1】:

    您可以在gridviewrowdatabound 函数上进行如下操作

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

    【讨论】:

    • 我取消了你的反对票,不知道你为什么是 DV。您的解决方案并不完整,但它正朝着正确的方向前进。另一个答案是标签的所有设置颜色,这不是 OP 要求的。
    • hi @Șhȇkhaṝ 但事件没有触发...,
    • @Anjali 你需要在gridviewOnRowDataBound="RowDataBound"Link上添加事件
    【解决方案2】:

    试试这个代码,每行的颜色都会随着类别或过滤器的变化而变化 http://devloper4u.blogspot.in/2013/10/how-to-set-color-in-every-row-on.html

    【讨论】:

      【解决方案3】:

      您需要处理 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";
          }
      }
      

      【讨论】:

        【解决方案4】:

        试试这样的

        <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 并在客户端实现相同的效果

        【讨论】:

        • 我会试着让你知道壁画
        • 你在哪里设置row-color
        • @Shekhar,我希望你知道如何使用 CSS。这里我添加到标签中,同样的东西也可以在行中应用。使用 CSS 类控制样式
        • 如何使用我认为不可能的代码设置行颜色。你需要去row data bound
        • @Shekhar,让我回到 2005 年,现在使用 GridView 示例,让您知道。我开始使用 ListView,它非常灵活。现在只有 MVC 而不是 Web 表单 :)
        【解决方案5】:
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            Control l = e.Row.FindControl("Label1");
          ((Label)l).BackColor = System.Drawing.Color.Red;
        }
        

        【讨论】:

        • 他想设置row color
        • 对,但是在那一行他有一个标签,所以他想设置标签背景颜色而不是行颜色...
        • 我在关于标签背景的问题中没有看到。
        猜你喜欢
        • 2015-09-28
        • 1970-01-01
        • 2013-11-29
        • 2011-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多