为DataGrid添加自动编号功能
[ 2005-06-03 21:46:00 | 作者: admin ]
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
<%@ 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
评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=108
这篇日志没有评论。
此日志不可发表评论。