关于DataGrid控件中的自动编号
点击次数:25 次 发布日期:2008-11-26 11:46:05 作者:源代码网
|
源代码网推荐 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> 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1487362 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
