首页 > 开发 > 综合 > 正文

为DataGrid添加自动编号功能

2024-07-21 02:23:07
字体:
来源:转载
供稿:网友
下面的代码实现在datagrid中添加自动编号的功能,主要是在数据绑定时利用item属性。

查看例子

datagridwithline.aspx

<%@ page language="vb" autoeventwireup="false" codebehind="datagridwithline.aspx.vb"
inherits="aspxweb.datagridwithline"%>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>datagridwithline</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:datagrid id="datagrid1" runat="server" autogeneratecolumns="false">
<headerstyle font-bold="true" wrap="false" horizontalalign="center"></headerstyle>
<columns>
<asp:templatecolumn></asp:templatecolumn>
<asp:boundcolumn datafield="title"></asp:boundcolumn>
<asp:boundcolumn datafield="createdate" dataformatstring="{0:yyyy-m-d h:m:s}"></asp:boundcolumn>
</columns>
</asp:datagrid>
</form>
</body>
</html>

datagridwithline.aspx.vb

imports system
imports system.data
imports system.data.oledb
public class datagridwithline
inherits system.web.ui.page
protected withevents datagrid1 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
datagrid1.columns(0).headertext = "序号"
datagrid1.columns(1).headertext = "文章标题"
datagrid1.columns(2).headertext = "创建日期"
dim cnstring as string = "provider=microsoft.jet.oledb.4.0;data source=" + server.mappath("test.mdb")
dim strsql as string = "select top 21 title,createdate from document order by createdate desc"
dim cn as new oledbconnection(cnstring)
cn.open()
dim cmd as new oledbcommand(strsql, cn)
dim db as oledbdatareader
db = cmd.executereader(commandbehavior.closeconnection)
datagrid1.datasource = db
datagrid1.databind()
cn.close()
cn = nothing
cmd = nothing
db.close()
db = nothing
end sub

private sub datagrid1_itemdatabound(byval sender as object, _
byval e as system.web.ui.webcontrols.datagriditemeventargs) handles datagrid1.itemdatabound
if e.item.itemindex <> -1 then
e.item.cells(0).text = e.item.itemindex + 1
end if
end sub
end class

  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表