在gridview的行背景颜色? [英] Row background color in 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屋!