DPC:Hiding Columns In A DataGrid[等级:初 中]
来源:互联网 发布:华为云计算工程师面试 编辑:程序博客网 时间:2024/05/21 06:49
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></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 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>
</htm
g/wangzhanyouhua/" title="seo,搜索引擎优化">seover="wIndow.status='正文--DPC:HidIng Columns In A DataGrid[等级:初 中]';return true">
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
HidIng Columns In A DataGridOne 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>
</htm
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- DPC:Hiding Columns In A DataGrid[等级:初 中]
- DPC: Creating a multicolumn Dropdownlist[等级:初 中]
- hiding a row in a cxGrid
- Merging columns in GridView/DataGrid header
- Easui中datagrid实现动态控制columns
- Easui中datagrid实现动态控制columns
- Encrypting columns in a database using jasypt
- Android: creating two columns in a linearlayout
- [R] Mean of columns in a data file
- Hiding Secrets in Android Apps
- Dragging and Dropping DataGrid Columns
- Dragging and Dropping DataGrid Columns
- Creating DataGrid Templated Columns Dynamically -
- DPC
- DPC
- WPF DataGrid DataGrid.Columns 使用笔记
- Find Text in all columns of all tables in a Database
- jquery easy UI datagrid新增一行,columns中editor下拉框之间的联动。
- 桌面中心(一) 创建数据库
- DPC: Creating a multicolumn Dropdownlist[等级:初 中]
- PHP 留言簿(带Oracle数据库分页的显示功能)
- C#如何打开关闭CDROM
- 一个广告轮播系统的例子(内含文件上传的方法)
- DPC:Hiding Columns In A DataGrid[等级:初 中]
- 用PHP发送MIME邮件(六)
- 转: ASP.NET高级应用 (1)
- 用PHP发送MIME邮件(五)
- ASP.NET 数据访问类
- 用PHP发送MIME邮件(三)
- Reading/Writing text files using C#(转:初学)
- 用PHP发送MIME邮件(四)
- DataGrid Web控件深度历险(3) part2