繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> Asp.Net开发 >> 给Repeater、Datalist和Datagrid增加自动编号列

给Repeater、Datalist和Datagrid增加自动编号列

2007-09-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介:号 内容 1 Taye 2 BOx 3 Glass 4 StarCraft 一、正序 A、AllowPaging=False情况下,使用以下方法就可以实现: 1 2 3 4 5 6 7 8 9 不过更有趣的方法是使用这个方法: 1 2 ...

内容

1

Taye

2

BOx

3

Glass

4

StarCraft

一、正序

A、AllowPaging=False情况下,使用以下方法就可以实现:

1

2

3

4

5 <%# Container.ItemIndex + 1%>

6

7

8

9

不过更有趣的方法是使用这个方法:

1

2

3

4

5 <%# this.DataGrid1.Items.Count + 1%>

6

7

8

9

也许有些人会觉得很奇怪为什么Items.Count会这样,而不是出来全部总合,但如果你了解绑定的过程时就容易理解。[从上面来看就是在ItemCreated事件中进行绑定所以得到的Items.Count刚好是当前的序号]

B、AllowPaging="True"下,如果DataGrid支持分页则可以如下:

1

2

3

4

5 <%# this.DataGrid1.CurrentPageIndex * this.DataGrid1.PageSize + Container.ItemIndex + 1%>

6

7

8

9

二、倒序的方法

序号

内容

4

Taye

3

BOx

2

Glass

1

StarCraft

由上面可以知道使用this.DataGrid1.Items.Count - Container.ItemIndex + 1方法是不可能实现的,得到值而且全会为1,分页的情况下更是一样.所以一开始我们就要取得数据源的行数:

1private int rowscount = 0;

2 protected int RowsCount

3 {

4 get{ return rowscount;}

5 set{ this.rowscount = value; }

6 }

7

8 private void Page_Load(object sender, System.EventArgs e)

9 {

10 // 在此处放置用户代码以初始化页面

11 if(!IsPostBack)

12 this.BindData();

13 }

14 private void BindData()

15 {

16 SqlConnection cn = new SqlConnection("server=(local);database=NorthWind;uid=sa;pwd=");

17 string str=@"SELECT Employees.EmployeeID, Orders.EmployeeID

18 FROM Employees INNER JOIN

19 Orders ON Employees.EmployeeID = Orders.EmployeeID ";

20 SqlDataAdapter sqlda = new SqlDataAdapter(str,cn);

21 DataSet ds = new DataSet();

22 sqlda.Fill(ds);

23 this.RowsCount = ds.Tables[0].Rows.Count;

24 this.DataGrid1.DataSource = ds;

25 this.DataGrid1.DataBind();

26}

1

2

3

4

5 <%# RowsCount - DataGrid1.CurrentPageIndex * DataGrid1.PageSize - Container.ItemIndex %>

6

7

8

9

责任编辑:admin
相关文章