Row background color in gridview?

試著忘記壹切 提交于 2019-12-13 01:30:21

问题


In asp.net application, I am using grid-view control in that I am binding the data to the label which is in grid-view.
If data is empty then the color of the row should be in red
If not I mean if data is there to bind then the row in green. This is my code:

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

回答1:


You need to handle the RowDataBound event, get into the e.Row item, and assign either a CSS class or directly set the background color. I prefer setting a CSS class so you can change the rendering of it without a recompile later.

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

And the code-behind, I had to assume you were using a DataTable as your data source, update the code to fit your data structure:

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



回答2:


You can do it on rowdatabound function of gridview as follows

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



回答3:


if(e.Row.RowType == DataControlRowType.DataRow)
{
    Control l = e.Row.FindControl("Label1");
  ((Label)l).BackColor = System.Drawing.Color.Red;
}



回答4:


Try something like this

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

Edit:

Instead of fighting with grid view inline and code behind stuffs, just use a jQuery and achieve the same in client side




回答5:


try it this code it is every row in color change with the category wise or filters wise http://devloper4u.blogspot.in/2013/10/how-to-set-color-in-every-row-on.html



来源:https://stackoverflow.com/questions/15082232/row-background-color-in-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!