当前位置: 首页 > 图文教程 > 网络编程 > ASP > 如何在DataGrid控件中隐藏列

ASP
从文本文件中读取信息并存储入数据库
文本搜索
FileSystemObject处理文件
设计 FileSystemObject
处理驱动器和文件夹
用ASP做一个TOP COOL的站内搜索
怎样读取一个文本文件的内容?
在线用表单建立文件夹
filesystemobject组件的用法示例
ASP中文本文件与数据库文件的数据交换(FSO)
读取目录下的所有文件(包括子目录下的所有文件)
ASP中FSO的神奇功能 - FSO不能做到的
ASP中FSO的神奇功能 - 用FSO进行内容管理
ASP中FSO的神奇功能 - 使用FSO进行搜索
ASP中FSO的神奇功能 - 权限许可
ASP中FSO的神奇功能 - 文件读取
ASP中FSO的神奇功能 - 写文件
ASP中FSO的神奇功能 - 简介
读取目录下的文件得到一个数组
奇妙的文件系统对象组件

ASP 中的 如何在DataGrid控件中隐藏列


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 164 ::
收藏到网摘: n/a

  前言:
asp.net在提供我们丰富的内置控件方便我们开发的同时,一些格式化的东西也限制了我们灵活的要求。在DataGrid控件中,我们遇到的一个非常典型的问题就是如何把我们不想显示的列根据需要随时隐藏掉。
解决方案:
我们不能隐藏在DataGrid里自动生成列的主要的一点原因是:DataGrid里的column不能被DataGrid的属性DataGridColumn增加。
基于以上原因,我们可以提出两种不同的方案来解决这个问题。
方案一:
根据页面请求的事件来隐藏列:
代码:
<%@ 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>
程序执行执行演示:
Show:

(图show)
Hide

(图hide)
方案二:

方案二实际上是一的变通,我就简单的介绍一下。在一中我们通过button的OnClick事件来判断是Show还是Hide,那么我们也可以通过联接获取参数的值来做判断。
代码:
<%@ 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" AutoGenerateCol