简体   繁体   English

如何使用 ASP.net gridview 显示/隐藏字段?

[英]How to show/hide fields with ASP.net gridview?

I inherited an ASP.net web application that displays data from a SQL database using the ASP.net GridView control. I inherited an ASP.net web application that displays data from a SQL database using the ASP.net GridView control.

The problem is that there are more than 20 fields in the table.问题是表中有20多个字段。 It is unlikely that the user would want to see all 20 fields at once, but it is equally implausible that any of the fields could be excluded from the display.用户不太可能希望同时查看所有 20 个字段,但同样令人难以置信的是,可以从显示中排除任何字段。

It seems like the solution would be to allow the user to select what fields to show and hide client-side.似乎解决方案是允许用户 select 显示和隐藏客户端的哪些字段。

Is there a good way to display and hide individual columns of an ASP.net GridView client-side ?有没有一种显示和隐藏ASP.net GridView客户端的各个列的好方法? If not, what kind of solution should I seek out?如果没有,我应该寻求什么样的解决方案?

I'm no javascript guru so I won't even attempt to answer that part of the question.我不是 javascript 大师,所以我什至不会尝试回答这部分问题。 But I have had a similar problem to yours, that being too many columns for the screen width you're working with.但是我遇到了与您类似的问题,即对于您正在使用的屏幕宽度而言,列太多了。

I would suggest grouping columns from your DB together into multi-field columns in the GridView using TemplateField.我建议使用 TemplateField 将数据库中的列分组到 GridView 中的多字段列中。 A lot of many-field data can be visually compressed this way.很多多字段数据可以通过这种方式进行可视化压缩。 I'll give you an example from a lookup tool I designed to work with our ManageEngine ServiceDesk installation:我将从我设计用于与我们的 ManageEngine ServiceDesk 安装一起使用的查找工具中为您提供一个示例:

        <asp:GridView ID="gridResults" runat="server" BorderColor="#223366" BorderWidth="0px"
            CellPadding="5" DataSourceID="dsResults" AutoGenerateColumns="False" DataKeyNames="ResID">
            <HeaderStyle BackColor="#223366" ForeColor="#F0F0F0" BorderColor="#223366" Font-Bold="true" />
            <RowStyle BackColor="#E0E0E0" ForeColor="#223366" BorderColor="#223366" HorizontalAlign="Left"
                VerticalAlign="Top" />
            <AlternatingRowStyle BackColor="#D0D0D0" ForeColor="#223366" BorderColor="#223366"
                HorizontalAlign="Left" VerticalAlign="Top" />
            <Columns>
                <asp:TemplateField HeaderText="Host Name">
                    <ItemTemplate>
                        <asp:Literal ID="Literal1" runat="server" Text='<%# "<a class=""BlueLink"" href=""https://mgmt-servicedesk.co.wood.wi.us/ViewWSDetails.do?wsId=" & Eval("ResID") & """>" & Eval("ResourceName") & "</a><br/>Serial #: " & Eval("SerialNo") & "<br/>Asset: " & Eval("AssetTag") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                        <asp:Literal ID="Literal1" runat="server" Text='<%# "Status: " & Eval("State") & "<br/>" & Eval("Manufacturer") & "<br/>" & Eval("Model") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Owner/Location">
                    <ItemTemplate>
                        <asp:Literal ID="Literal1" runat="server" Text='<%# "User: " & Eval("UserName") & "<br/>Dept: " & Eval("Department") & "<br/>" & Eval("Location")   %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <%# RenderDameWareLinks(Eval("ResourceName"))%>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EmptyDataTemplate>
                No Results
            </EmptyDataTemplate>
        </asp:GridView>

When rendered, this is the what the gridview looks like (sorry for so much blurring, can't risk posting our internal records):渲染后,这就是 gridview 的样子(抱歉模糊不清,不能冒险发布我们的内部记录):

在此处输入图像描述

I have several advanced search pages in my company's intranet site, and I provide checkboxes for every column.我公司的 Intranet 站点中有几个高级搜索页面,并且我为每一列提供了复选框。 The default's are selected at pageLoad, but the user can change the ones they want before running the search.在 pageLoad 中选择默认值,但用户可以在运行搜索之前更改他们想要的值。 Then the sql is generated dynamically in code-behind, and the sqlDataSource is updated.然后在代码隐藏中动态生成 sql,并更新 sqlDataSource。 The grid should be set to automatically generate columns, then your good to go.网格应该设置为自动生成列,然后你的好到go。

EDIT: Sorry, I didn't see the client-side requirement at first.编辑:对不起,我一开始没有看到客户端的要求。 Although with an asp.net Update Panel, my solution looks like quick client-side code to the user.尽管使用 asp.net 更新面板,但我的解决方案对用户来说就像是快速的客户端代码。 You can even update the grid each time a column is checked or unchecked.您甚至可以在每次选中或取消选中列时更新网格。

If my solution is of interest, let me know and I can post code examples.如果我的解决方案感兴趣,请告诉我,我可以发布代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM