关于DataGrid等控件中的自动编号

 

网上有很多人问关于DataGrid的自动编号问题,但在论坛中我已经回复过好几次,但还是不断有人问

序号
 内容
 
1
 Taye
 
2
 BOx
 
3
 Glass
 
4
 StarCraft
 

 

一、正序

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>

 
 

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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值