DPC:Hiding Columns In A DataGrid[等级:初 中]
2024-07-21 02:16:49
供稿:网友
hiding columns in a datagrid
one of the frequently asked questions over at asplists.com is: "how do i hide a column in a datagrid?". one very important point to note is that you cannot hide autogenerated columns in a data grid. the reason for this is that autogenerated columns are not added to the datagrid's datagridcolumn collection. so, in order for a column to be hidden it must be either programmatically added to the datagrid at runtime or explicitly defined (using templates) at design time with the autogeneratecolumns property set to false (the code in this article will use this method).
this article provides sample code to hide a column (could easily be modified to hide more) for two different scenarios - hiding columns in response to an event on the page (common in web reports) or hiding columns to provide different functionality based on security levels.
first, let's look at how we can hide and show a column in a datagrid in response to an event (the click of a button) on the page. you can see a live example here.
the code:
<%@ page language="vb" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sqlclient" %>
<html>
<script runat="server">
sub page_load(sender as object, e as eventargs)
dim myconnection as sqlconnection = new _
sqlconnection("data source=(local)/netsdk; trusted_connection=yes; initial catalog=pubs")
dim mycommand as sqlcommand = new sqlcommand("select * from publishers", myconnection)
myconnection.open()
mydatagrid.datasource = mycommand.executereader(commandbehavior.closeconnection)
mydatagrid.databind()
end sub
sub hideshow_click(sender as object, e as eventargs)
if mydatagrid.columns(0).visible = false then
mydatagrid.columns(0).visible = true
else
mydatagrid.columns(0).visible = false
end if
end sub
</script>
<body>
<form runat="server">
<asp:datagrid id="mydatagrid" width="25%" autogeneratecolumns="false" runat="server">
<columns>
<asp:templatecolumn headertext="publisher's id">
<itemtemplate>
<span><%# container.dataitem("pub_id") %></span>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="publisher's name">
<itemtemplate>
<span><%# container.dataitem("pub_name") %></span>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="city">
<itemtemplate>
<span><%# container.dataitem("city") %></span>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="state">
<itemtemplate>
<span><%# container.dataitem("state") %></span>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="country">
<itemtemplate>
<span><%# container.dataitem("country") %></span>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
<asp:button id="hideshow" text="hide/show" onclick="hideshow_click" runat="server" />
</form>
</body>
</html>
next, let's look at how we can hide a column in a datagrid based on the security level of the user. this example is not *actually* secure and security is not discussed in this article - we are trying to show how to hide datagrid columns not how to secure a page! the code below checks a querystring parameter called 'security' each time the page loads. if 'security' equals "admin" then all of the columns are displayed. if 'security' equals anything other than "admin" then the first column in the datagrid is hidden.
click here to view the datagrid with no priveleges. (no pub_id column)
click here to view the datagrid with admin priveleges. (all columns visible)
the code:
<%@ page language="vb"%>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sqlclient" %>
<html>
<script runat="server">
sub page_load(sender as object, e as eventargs)
dim myconnection as sqlconnection = new _
sqlconnection("data source=(local)/netsdk; trusted_connection=yes; initial catalog=pubs")
dim mycommand as sqlcommand = new sqlcommand("select * from publishers", myconnection)
myconnection.open()
mydatagrid.datasource = mycommand.executereader(commandbehavior.closeconnection)
mydatagrid.databind()
if request.querystring("security") = "admin" then
mydatagrid.columns(0).visible = false
end if
end sub
</script>
<body>
<form runat="server">
<asp:datagrid id="mydatagrid" autogeneratecolumns="false" runat="server">
<columns>
<asp:templatecolumn headertext="publisher's id">
<itemtemplate>
<span><%# container.dataitem("pub_id") %></span>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="publisher's name">
<itemtemplate>
<span><%# container.dataitem("pub_name") %></span>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="city">
<itemtemplate>
<span><%# container.dataitem("city") %></span>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="state">
<itemtemplate>
<span><%# container.dataitem("state") %></span>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="country">
<itemtemplate>
<span><%# container.dataitem("country") %></span>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
</form>
</body>
</html>