教程中国
PHOTOSHOP CS9.0中文版 MAYA 8.5 FOR WINDOWS Corel Painter v9.0 Flash MX2004 中文版 Illustrator cs2 中文版
VC++6.0含sp6 中英文版 VB6.0 +sp6 简体中文版 Borland Delphi 7汉化版 MSDN for vb6.0中文版 Visual Studio 2005简体
教程中国下属 文件存储共享专家BIBIDU.COM 提供大型软件,教材,源码,电影,音乐,图书等下载 更多精品请点此进入
  您目前所在位置: 教程中国 >> .NET类 >> ASP.NET >> 为DataGrid添加自动编号功能 RSS订阅
为DataGrid添加自动编号功能
教程(视频,书籍)下载:  ASP.NET AutoCAD 数据库 C# ASP java photoshop 网页设计 delphi 3dmax Flash C++ VB 张孝祥 实例   更多请进入BIBIDU搜索
IT搜索引擎   
下面的代码实现在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



来源:upschool.com.cn
作者:
关键字:
发表日期:2005-12-23

网页显示有限 阅读全文请下载本文完整版WORD文档

上一篇:利用OleDb对象,将数据库中全部表转换成XML文件   下一篇:在ASP.NET中使用Office Web Components (OWC)创建统计图


2009-1-9 19:32:00
本文的相类似文章
在学习中进步 在进步中成长 教程中国相随您的成长之路
华腾联合科技股份有限公司版权所有
广告联系:Rosibo@163.com