为DataGrid添加自动编号功能 (转)

为DataGrid添加自动编号功能 (转)[@more@]

为DataGrid添加自动编号功能

do.NETplayground.com/">http://lucky_elove.www1.dotnetplayground.com/

下面的代码实现在DataGrid中添加自动编号的功能,主要是在数据绑定时利用Item属性。

ASPx">查看例子

DataGridWithLine.aspx

web.DataGridWithLine"%> w3c//DTD HTML 4.0 Transitional//EN"> DataGridWithLinemicrosoft Visual Studio .net 7.0"> Visual Basic 7.0"> Javascript"> intellisense/ IE5"> RM id="Form1" method="post" runat="server">

DataGridWithLine.aspx.vb

Imports System Imports System.Data Imports System.Data.Ole db Public Class DataGridWithLine Inherits System.Web.UI.Page Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid #Region " Web 窗体设计器生成的代码 " '该 调用是 Web 窗体设计器所必需的。 buggerStepThrough()> 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 = "Prov ider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath("Test.mdb") Dim str sql 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

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752019/viewspace-958427/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752019/viewspace-958427/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值