Dpc:hiding Columns in A datagrid[Grade: Junior High School]
Source: Internet
Author: User
DataGrid hiding Columns in A DataGrid
One of the frequently asked questions over at Asplists.com are: "How does I hide a column in a DataGrid?". One very important point to "that" you cannot hide autogenerated columns in a data grid. The reason is autogenerated columns are not added to the DataGrid ' s DataGridColumn collection. So, in order for a column to is hidden it must is either programmatically added to the DataGrid at runtime or explicitly d efined (using templates) at the AutoGenerateColumns property set to False (the code in this article would Use this method).
This is Article provides sample code to hide a column (could easily is modified to hide more) for two different IDing columns in response to a event on the page (common in Web reports) or hiding columns to provide different Lity based on the security levels.
A, 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 th E page. You can have a live example here.
The code:
<%@ Page language= "VB"%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SqlClient"%>
<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>
Next, let's look at how we can hide a column in a DataGrid based to the security level of the user. This example isn't *actually* secure and security isn't discussed in this article-we are the to show how to hide Da Tagrid columns to secure a page! The code below checks a querystring parameter called ' Security ' is the page loads. If ' security ' equals ' Admin ' then all of the columns are displayed. If ' security ' equals anything other than "Admin" then the "the" the "the" the "the", "the" 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"%>
<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)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service