顯示和隱藏DataGrid中的列
2024-07-21 02:23:08
供稿:網友
要顯示和隱藏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