<script type="text/javascript">
</script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
One of the frequently asked questions over at Asplists.com is: "How do I hide a column In a DataGrid?". One very important po Int 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 def Ined (us Ing templates) at design time with the AutoGenerate Columns 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 - Hid Ing Columns In response to an event on the page (common In web reports) or Hid Ing 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()
my DataGrid.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
my DataGrid.DataB Ind()
End Sub
Sub HideShow_Click(Sender As Object, E As EventArgs)
If my DataGrid. Columns(0).Visible = False Then
my DataGrid. Columns(0).Visible = True
Else
my DataGrid. Columns(0).Visible = False
End If
End Sub
</script>
<body>
<form runat="server">
<asp: DataGrid id="my DataGrid" Width="25%" AutoGenerate Columns="false" runat="server">
< Columns>
<asp:TemplateColumn HeaderText="Publisher's ID">
<ItemTemplate>
<span><%# Conta Iner.DataItem("pub_id") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Publisher's Name">
<ItemTemplate>
<span><%# Conta Iner.DataItem("pub_name") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="City">
<ItemTemplate>
<span><%# Conta Iner.DataItem("city") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="State">
<ItemTemplate>
<span><%# Conta Iner.DataItem("state") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Country">
<ItemTemplate>
<span><%# Conta Iner.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 try Ing to show how to hide DataGrid Columns not how to secure a page! The code below checks a Querystr Ing parameter called 'Security' each time the page loads. If 'Security' equals "Adm In" then all of the Columns are displayed. If 'Security' equals anyth Ing other than "Adm In" 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 Adm In 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()
my DataGrid.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
my DataGrid.DataB Ind()
If Request.QueryStr Ing("Security") = "Adm In" Then
my DataGrid. Columns(0).Visible = False
End If
End Sub
</script>
<body>
<form runat="server">
<asp: DataGrid id="my DataGrid" AutoGenerate Columns="false" runat="server">
< Columns>
<asp:TemplateColumn HeaderText="Publisher's ID">
<ItemTemplate>
<span><%# Conta Iner.DataItem("pub_id") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Publisher's Name">
<ItemTemplate>
<span><%# Conta Iner.DataItem("pub_name") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="City">
<ItemTemplate>
<span><%# Conta Iner.DataItem("city") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="State">
<ItemTemplate>
<span><%# Conta Iner.DataItem("state") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Country">
<ItemTemplate>
<span><%# Conta Iner.DataItem("country") %></span>
</ItemTemplate>
</asp:TemplateColumn>
</ Columns>
</asp: DataGrid>
</form>
</body>
</htm
Ing/wangzhanyouhua/" title="seo,搜索引擎优化">seover="w
Indow.status='正文--
DPC:
Hid
Ing
Columns
In A
DataGrid[
等级:初 中]';return true">
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript">
</script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
Hid
Ing
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 po Int 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 def Ined (us Ing templates) at design time with the AutoGenerate Columns 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 - Hid Ing Columns In response to an event on the page (common In web reports) or Hid Ing 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()
my DataGrid.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
my DataGrid.DataB Ind()
End Sub
Sub HideShow_Click(Sender As Object, E As EventArgs)
If my DataGrid. Columns(0).Visible = False Then
my DataGrid. Columns(0).Visible = True
Else
my DataGrid. Columns(0).Visible = False
End If
End Sub
</script>
<body>
<form runat="server">
<asp: DataGrid id="my DataGrid" Width="25%" AutoGenerate Columns="false" runat="server">
< Columns>
<asp:TemplateColumn HeaderText="Publisher's ID">
<ItemTemplate>
<span><%# Conta Iner.DataItem("pub_id") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Publisher's Name">
<ItemTemplate>
<span><%# Conta Iner.DataItem("pub_name") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="City">
<ItemTemplate>
<span><%# Conta Iner.DataItem("city") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="State">
<ItemTemplate>
<span><%# Conta Iner.DataItem("state") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Country">
<ItemTemplate>
<span><%# Conta Iner.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 try Ing to show how to hide DataGrid Columns not how to secure a page! The code below checks a Querystr Ing parameter called 'Security' each time the page loads. If 'Security' equals "Adm In" then all of the Columns are displayed. If 'Security' equals anyth Ing other than "Adm In" 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 Adm In 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()
my DataGrid.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
my DataGrid.DataB Ind()
If Request.QueryStr Ing("Security") = "Adm In" Then
my DataGrid. Columns(0).Visible = False
End If
End Sub
</script>
<body>
<form runat="server">
<asp: DataGrid id="my DataGrid" AutoGenerate Columns="false" runat="server">
< Columns>
<asp:TemplateColumn HeaderText="Publisher's ID">
<ItemTemplate>
<span><%# Conta Iner.DataItem("pub_id") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Publisher's Name">
<ItemTemplate>
<span><%# Conta Iner.DataItem("pub_name") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="City">
<ItemTemplate>
<span><%# Conta Iner.DataItem("city") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="State">
<ItemTemplate>
<span><%# Conta Iner.DataItem("state") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Country">
<ItemTemplate>
<span><%# Conta Iner.DataItem("country") %></span>
</ItemTemplate>
</asp:TemplateColumn>
</ Columns>
</asp: DataGrid>
</form>
</body>
</htm
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript">
</script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>