如何在gridview中隐藏和显示列

时间:2015-08-06 06:24:53

标签: c# asp.net c#-4.0 gridview

我在下面写了一行代码,以便根据条件隐藏和显示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>

它没有用!

2 个答案:

答案 0 :(得分:1)

代码更新如下:

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

答案 1 :(得分:0)

网格视图

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