asp.net中显示DataGrid控件列序号的几种方法

** asp.net ** ** 中显示 DataGrid ** ** 控件列序号的几种方法 **

作者:郑佐 2004-9-10

在 aps.net 中多数据绑定的控件很多,论功能来说,应该属 DataGrid 最为齐全,但它没有提供现成的显示记录序号的功能,不过我们可以通过它所带的一些参数来间接得到序号,下面来看看怎样得到和显示序号值计算方式如下:

(1) 在后台

DataGrid .CurrentPageIndex * DataGrid .PageSize + e.Item.ItemIndex + 1

(2) 在前台

DataGrid1.CurrentPageIndex * DataGrid1.PageSize + Container.ItemIndex + 1

说明:

e 表示 System.Web.UI.WebControls.DataGridItemEventArgs 参数类的实例;

DataGrid1 这里表示前台的一个实例;

DataGrid.CurrentPageIndex :获取或设置当前显示页的索引;

DataGrid.PageSize :获取或设置要在 DataGrid 控件的单页上显示的项数。

下面我使用了 4 种方法来在前台显示序号,不过都是围绕上面的计算式展开。

(1) 使用 DataGrid 的 ItemCreated 设置值,而前台的单元格可以是绑定列或者模板列 ( 包括空模板 ) ;

(2) 使用 DataGrid 的 ItemDataBound 设置值,而前台的单元格可以是绑定列或者模板列 ( 包括空模板 ) ;

(3) 在前台直接绑定计算表达式;

(4) 在后台类中编写方法计算表达式由前台页面类继承调用。

备注:在数据库中获取数据时设置额外的序号列这里不做讨论,我认为这是最糟糕的实现方法。

下面以获取 Northwind 数据库的 Customers 表的数据为列,显示如下:

|

序号 1

|

序号 2

|

序号 3

|

序号 4

|

序号 5

|

CustomerID

---|---|---|---|---|---

51

|

51

|

51

|

51

|

51

|

LONEP

52

|

52

|

52

|

52

|

52

|

MAGAA

53

|

53

|

53

|

53

|

53

|

MAISD

54

|

54

|

54

|

54

|

54

|

MEREP

55

|

55

|

55

|

55

|

55

|

MORGK

56

|

56

|

56

|

56

|

56

|

NORTS

57

|

57

|

57

|

57

|

57

|

OCEAN

58

|

58

|

58

|

58

|

58

|

OLDWO

59

|

59

|

59

|

59

|

59

|

OTTIK

60

|

60

|

60

|

60

|

60

|

PARIS

1 2 3 4 5 6 7 8 9 10

下面是 WebFormPaging.aspx 文件代码,

1@ Page language="c#" Codebehind="WebFormPaging.aspx.cs" AutoEventWireup="false" Inherits="AspnetPaging.WebForm1" 
 1<html>
 2<head>
 3<title>WebForm1</title>
 4<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
 5<meta content="C#" name="CODE_LANGUAGE"/>
 6<meta content="JavaScript" name="vs_defaultClientScript"/>
 7<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"/>
 8</head>
 9<body>
10<form id="Form1" method="post" runat="server">
11<table align="center" border="1" cellpadding="1" cellspacing="1" id="Table1" width="400">
12<tr>
13<td><asp:datagrid allowpaging="True" autogeneratecolumns="False" id="DataGrid1" runat="server" width="100%">
14<columns>
15<asp:boundcolumn headertext="  序号  1"></asp:boundcolumn>
16<asp:templatecolumn headertext="  序号  2"></asp:templatecolumn>
17<asp:templatecolumn headertext="  序号  3">
18<itemtemplate>
19<asp:label id="itemIndex" runat="server"></asp:label>
20</itemtemplate>
21</asp:templatecolumn>
22<asp:templatecolumn headertext="  序号  4">
23<itemtemplate>

(DataGrid1.CurrentPageIndex * DataGrid1.PageSize + Container.ItemIndex + 1)

1
2</itemtemplate>
3</asp:templatecolumn>
4<asp:templatecolumn headertext="  序号  5">
5<itemtemplate>

GetRecordIndex( Container.ItemIndex )

 1
 2</itemtemplate>
 3</asp:templatecolumn>
 4<asp:boundcolumn datafield="CustomerID" headertext="CustomerID"></asp:boundcolumn>
 5</columns>
 6<pagerstyle mode="NumericPages"></pagerstyle>
 7</asp:datagrid></td>
 8</tr>
 9<tr>
10<td></td>
11</tr>
12<tr>
13<td></td>
14</tr>
15</table>
16</form>
17</body>
18</html>

后台 WebFormPaging.aspx.cs 代码如下:

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

&nb

Published At
Categories with Web编程
Tagged with
comments powered by Disqus