我的一亩三分地 我就喜欢!
13fen  设为主页
 收藏本站
 
当前位置: > 一亩三分地:首页 > 网络学院 > 网络编程 > ASP专区 > Asp数据库/打印 > 在DataGrid等控件中添加自动编号的列
热门文章排行
热门文章排行 手推车”功能的实现(10-07)
八大法则防范ASP网站漏洞(10-23)
ASP教程十一、调试ASP脚本(10-23)
在JSP中访问数据库大全(10-23)
虚机服务中常见Asp.Net低级错误一览(03-21)
精采文章排行
精采文章排行 ASP.NET实现抓取网页中的链接(11-15)
ASP连接数据库的11种方法(11-10)
如何动态创建网页的RSS内容摘要(11-10)
ASP网站漏洞及入侵防范方法(11-10)
ASP自定义函数:对字符串正则替换(11-10)
技术专题推荐
网管论坛交流
 

在DataGrid等控件中添加自动编号的列 

作者:佚名   来源:一亩三分地   点击:   日期:2007-03-23

一、正序
A、AllowPaging=False情况下



<asp:DataGrid id="DataGrid1" runat="server">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%# Container.ItemIndex + 1%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>



就可以实现

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



<asp:DataGrid id="DataGrid1" runat="server">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%# this.DataGrid1.Items.Count + 1%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>




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

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



<asp:DataGrid id="DataGrid1" runat="server" AllowPaging="True">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%# this.DataGrid1.CurrentPageIndex * this.DataGrid1.PageSize + Container.ItemIndex + 1%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>




二、倒序的方法

序号
内容

4
Taye

3
BOx

2
Glass

1
StarCraft


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

.cs

private int rowscount = 0;

protected int RowsCount

{

get{ return rowscount;}

set{ this.rowscount = value; }

}



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

{

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

if(!IsPostBack)

this.BindData();

}



private void BindData()

{

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

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

FROM Employees INNER JOIN

Orders ON Employees.EmployeeID = Orders.EmployeeID ";



SqlDataAdapter sqlda = new SqlDataAdapter(str,cn);

DataSet ds = new DataSet();



sqlda.Fill(ds);



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



this.DataGrid1.DataSource = ds;

this.DataGrid1.DataBind();



}






.aspx

<asp:DataGrid id="DataGrid1" runat="server" AllowPaging="True">

<Columns>

<asp:TemplateColumn>

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateColumn>

</Columns>

</asp:DataGrid>




当然如果是不是分页的情况一下更容易实现了.




文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【论坛讨论

   相关文章:
·ASP中巧用Response属性 ·第六课:ASP脚本循环语句
·在 Web 页上使用条件数值格式 ·连接数据库查询手册(不仅仅适用于asp)
·警惕"给你的FileSystemObject对象加把锁" ·用ASP做全文检索

   文章评论:(条)
  
 请留名: 匿名评论   点击查看所有评论 网管论坛
 

  责任编辑:一分  声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。