How to hide columns in the DataGrid control

Source: Internet
Author: User
Tags execution
How Datagrid|datagrid controls Hide columns in the DataGrid control
Objective:
ASP.net in providing our rich built-in controls to facilitate our development, some of the formatting also limits our flexible requirements. One of the very typical problems we encounter in the DataGrid control is how to hide the columns we don't want to display at any time as needed.
Solution:
We can't hide in the DataGrid. The main reason for automatically generating columns is that the column in the DataGrid cannot be datagridcolumn by the properties of the DataGrid.
Based on the above reasons, we can propose two different schemes to solve this problem.
Programme I:
Hide columns based on the events requested by the page:
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>
Program Execution Execution Demo:
Show:

(Figure Show)
Hide

(Figure Hide)
Programme II:

Scenario Two is actually a workaround, and I'll just give you a brief introduction. In one we judge the show or hide by the button's OnClick event, so we can also use the join to get the value of the parameter to judge.
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 ()
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>
Program Execution Demo:
Value of the 1.Security parameter: nothing

(Figure: Hidde)
Security Parameters: Admin

(Photo: Show)

CNet transferred from: http://www.aspalliance.com/Full text

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.