首页 > 开发 > 综合 > 正文

显示和隐藏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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表