显示和隐藏DataGrid中的列

类别:.NET开发 点击:0 评论:0 推荐:
显示和隐藏DataGrid中的列
作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2003年5月4日 2点2分40秒

要显示和隐藏DataGrid中的列,最关键的是AutoGenerateColumns设置为False:下面就是实现这一功能的aspx代码和脚本代码:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ShowHideCols.aspx.vb" Inherits="aspxWeb.ShowHideCols"%> <!DOCTYPE html PUBLIC "-//W3C//DTD html 4.0 Transitional//EN"> <HTML> <HEAD> <title>ShowHideCols</title> <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:Button ID="btnShow" Text="Show Details" OnClick="ShowDetails" Runat="server" /> <asp:Button ID="btnHide" Text="Hide Details" OnClick="HideDetails" Runat="server" /> <asp:DataGrid ID="dtgCusts" Runat="server" AutoGenerateColumns="False" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3" GridLines="Vertical"> <Columns> <asp:BoundColumn DataField="Title" /> <asp:BoundColumn DataField="id" Visible="False" /> <asp:BoundColumn DataField="CreateDate" DataFormatString="{0:yyyy-MM-dd HH:mm:ss}" Visible="False" /> <asp:EditCommandColumn EditText="Edit" HeaderText="Edit" Visible="False" /> </Columns> <AlternatingItemStyle BackColor="#DCDCDC" /> <ItemStyle ForeColor="Black" BackColor="#EEEEEE" /> <headerStyle Font-Bold="True" ForeColor="White" BackColor="#000084" /> </asp:DataGrid> </form> </body> </HTML>

后代码脚本

Imports System.Data Imports System.Data.OleDb Public Class ShowHideCols Inherits System.Web.UI.Page Protected WithEvents btnShow As System.Web.UI.WebControls.Button Protected WithEvents btnHide As System.Web.UI.WebControls.Button Protected WithEvents dtgCusts As System.Web.UI.WebControls.DataGrid #Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs)_ Handles MyBase.Init 'CODEGEN: 此方法调用是 Web 窗体设计器所必需的 '不要使用代码编辑器修改它。 InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_ Handles MyBase.Load '在此处放置初始化页的用户代码 btnShow.Text = "显示列" btnHide.Text = "隐藏列" dtgCusts.Columns(1).HeaderText = "" dtgCusts.Columns(0).HeaderText = "标题" dtgCusts.Columns(2).HeaderText = "发布日期" dtgCusts.Columns(3).HeaderText = "编辑" If Not IsPostBack Then BindTheData() End If End Sub Sub BindTheData() Dim objConn As OleDbConnection Dim objCmd As OleDbCommand objConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _ + Server.MapPath("Test.mdb")) Dim strSql As String strSql = "SELECT Top 10 id,Title,CreateDate FROM Document" objCmd = New OleDbCommand(strSql, objConn) objConn.Open() dtgCusts.DataSource = objCmd.ExecuteReader() dtgCusts.DataBind() objConn.Close() objConn.Dispose() End Sub Sub ShowDetails(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim intCounter As Integer For intCounter = 1 To dtgCusts.Columns.Count - 1 dtgCusts.Columns(intCounter).Visible = True Next End Sub Sub HideDetails(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim intCounter As Integer For intCounter = 1 To dtgCusts.Columns.Count - 1 dtgCusts.Columns(intCounter).Visible = False Next End Sub End Class

本文地址:http://com.8s8s.com/it/it43187.htm