在gridview的行背景颜色? [英] Row background color in gridview?

查看:135
本文介绍了在gridview的行背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在asp.net应用程序,我使用网格视图控件中,我将数据绑定到标签这是并网查看

如果数据为空,然后该行的颜色应为红色

如果不是我的意思是如果数据存在绑定,然后在行绿色
这是我的code:

 < ASP:的TemplateField的HeaderText =度假区>
     <&ItemTemplate中GT;
         < ASP:标签ID =lblholdareg=服务器文本='<%#的eval(Holidaregion)%>' >
         < / ASP:标签>
     < / ItemTemplate中>
< / ASP:的TemplateField>


解决方案

您需要处理RowDataBound事件,进入e.Row项目,并分配任意一个CSS类或直接设置背景颜色。我preFER设置一个CSS类,所以你可以改变它的渲染,而不重新编译后。

 < ASP:GridView控件ID =GridView1=服务器OnRowDataBound =GridView1_RowDataBound>
    <柱体和GT;
        < ASP:的TemplateField的HeaderText =度假区>
            <&ItemTemplate中GT;
                < ASP:标签ID =lblholdareg=服务器文本='<%#的eval(Holidaregion)%>' />
            < / ItemTemplate中>
        < / ASP:的TemplateField>
    < /专栏>
< / ASP:GridView的>

而code-后面,我只好假设你正在使用一个DataTable作为数据源,更新code,以适应你的数据结构:

 保护无效GridView1_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
    连续的System.Data.DataRow =(的System.Data.DataRow)e.Row.DataItem;
    如果(行[Holidaregion] == NULL ||行[Holidaregion]。的ToString()。修剪()。长度== 0)
    {
        e.Row.CssClass =行空;
    }
    其他
    {
        e.Row.CssClass =排满;
    }
}

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>

解决方案

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

这篇关于在gridview的行背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆