如何在gridview中隐藏和显示列
本文关键字:显示 隐藏 gridview | 更新日期: 2023-09-27 18:06:32
我已经编写了下面的代码行,以便在gridview中隐藏和显示基于条件的列。
<asp:TemplateField HeaderText="FirstName" Visible='<%# Eval("FirstName") != null ? true:false %>'>
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
它不工作!
按如下代码更新:
<asp:TemplateField HeaderText="FirstName" Visible='<%= !string.IsNullOrEmpty(Eval("FirstName")) ? "true" : "false" %>'>
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
编辑
<asp:TemplateField HeaderText="FirstName" Visible='<%# !string.IsNullOrEmpty(Eval("FirstName")) ? "true" : "false" %>'>
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
网格视图
<asp:GridView ID="GridDisplay" runat="server" AutoGenerateColumns="False" BorderStyle="Groove"
BorderWidth="1px" BorderColor="ActiveCaptionText" Width="100%" EmptyDataText="No Record Found"
AllowPaging="True" OnPageIndexChanging="GridDisplay_PageIndexChanging" AllowSorting="True"
OnRowEditing="GridDisplay_RowEditing" OnRowDeleting="GridDisplay_RowDeleting"
CssClass="listbooking ListingTable">
<Columns>
<asp:TemplateField HeaderText="Booking Code">
<ItemTemplate>
<%# Eval("id")%>
</ItemTemplate>
<HeaderStyle Width="5%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Property Code">
<ItemTemplate>
<%# Eval("property_id")%>
</ItemTemplate>
<HeaderStyle Width="5%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("sales_username")%>
</ItemTemplate>
<HeaderStyle Width="18%" />
</asp:TemplateField>
</Columns>
</asp:GridView>
我正在尝试隐藏2列(名称)。
绑定网格数据时
this.GridDisplay.Columns[2].Visible = false;