程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 顯示和隱藏 DataGrid中的列

顯示和隱藏 DataGrid中的列

編輯:.NET實例教程

     要顯示和隱藏DataGrid中的列,最關鍵的是AutoGenerateColumns設置為False:下面就是實現這一功能的ASPx代碼和腳本代碼:
  
  <%@ Page Language="<a href="http://dev.21tx.com/language/vb/" target="_blank">VB</a>" AutoEventWireup="false" Codebehind="ShowHideCols.ASPx.vb"
  Inherits="ASPx<a href="http://dev.21tx.com/web/" target="_blank">Web</a>.ShowHideCols"%>
  <!DOCTYPE html PUBLIC "-//W3C//DTD Html 4.0 Transitional//EN">
  <Html>
   <HEAD>
   <title>ShowHideCols</title>
   <meta name="GENERATOR" content="Microsoft Visual Studio<a href="http://dev.21tx.com/dotnet/" target="_blank">.Net</a> 7.0">
   <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
   <meta name="vs_defaultClIEntScript" content="<a href="http://dev.21tx.com/web/Javascript/" target="_blank">JavaScript</a>">
   <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" Border 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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved