为DataGrid添加自动编号功能

摘要:
下面的代码实现在DataGrid中添加自动编号的功能,主要是在数据绑定时利用Item属性。DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN"˃DataGridWithLineDataGridWithLine.aspx.vbImportsSystemImportsSystem.DataImportsSystem.Data.OleDbPublicClassDataGridWithLineInheritsSystem.Web.UI.PageProtectedWithEventsDataGrid1AsSystem.Web.UI.WebControls.DataGrid#Region"Web窗体设计器生成的代码"'该调用是Web窗体设计器所必需的。

下面的代码实现在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 method="post" runat="server">
<asp:DataGrid 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 </system.diagnostics.debuggerstepthrough>

免责声明:文章转载自《为DataGrid添加自动编号功能》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇etl工具,kettle实现循环使用Java编写TCP协议发送和接收数据接口下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

wpf 控件绑定鼠标命令、键盘命令

1 <Window x:Class="CommandDemo.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winf...

WPF DataGrid多选功能开发

<DataTemplate x:Key="CheckBoxDataTemplate"> <Grid x:Name="Grid" HorizontalAlignment="Center" VerticalAlignment="Center"> <CheckBox...

ASP.NET Web应用程序修改页面Inherits示例

<@page 中 Codebehind 、Inherits 和aspx的关系 CodeBehind 指定包含与页关联的类的已编译文件的名称。该属性不能在运行时使用。 说明: 提供此属性是为了与以前版本的 ASP.NET 的兼容,以实现代码隐藏功能。在 ASP.NET 2.0 版中,应改用 CodeFile 属性指定该源文件的名称,同时使用 Inher...

一、Linux 安装

Linux 安装本章节我们将为大家介绍Linux的安装。 本章节以 centos6.4 为例。 centos 下载地址: 可以去官网下载最新版本:https://www.centos.org/download/ 以下针对各个版本的ISO镜像文件,进行一一说明: CentOS-7.0-x86_64-DVD-1503-01.iso : 标准安装版,一般下...

WPF DataGrid 绑定DataSet数据 自动生成行号

1、绑定数据:dataGrid1.ItemsSource = dataSet.Tables[0].DefaultView; 注意:在创建DataGrid 时可以通过AutoGenerateColumns 属性设置列是否自动生成,从而加入自定义列。如果DataGrid 中同时包含“自动生成列”与“用户自定义列”,则首先创建“用户自定义列”。DataGrid...

关于.Net中使用SQLite数据库的方法

参考: SQLite之C#连接SQLite https://www.cnblogs.com/icebutterfly/p/7850689.html 关于SQLite的库安装比较特殊: 下载地址:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki --ok! htt...