文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>GridView生成序号

GridView生成序号

时间:2010-09-26  来源:/* eXplorer :D */

一个经常碰到的情况:GridView需要添加一个序号列,并且从1开始自动编号。而数据库中的ID往往是不连续的(会有记录被删除的情况),我们无法绑定现有字段作为编号。因此我们需要手动给GridView编号。

思路:在GridView每一行,既Row,被生成的时候,在序号单元格中输出该行的ID+1(第一行是从0开始编的,因此要+1)

实现:

首先,在ASPX页面中,需要手动给GridView添加一个BoundField。示例代码如下:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EnableModelValidation="True"
OnRowCreated
="GridView1_RowCreated">
<Columns>
<asp:BoundField HeaderText="序号">
<HeaderStyle Height="24px" />
<ItemStyle Font-Size="14px" Height="24px" />
</asp:BoundField>
<asp:BoundField DataField="Sn" HeaderText="卡号" />
<asp:BoundField HeaderText="货物规格" />
<asp:BoundField HeaderText="数量" />
<asp:BoundField DataField="GhsId" HeaderText="供货商ID" />
<asp:BoundField DataField="Shr" HeaderText="收货人姓名" />
<asp:BoundField DataField="ShrContact" HeaderText="收货人电话" />
<asp:BoundField DataField="ShAddress" HeaderText="送货地址" />
<asp:BoundField HeaderText="物流单号" />
<asp:BoundField DataField="Bz" HeaderText="备注" />
</Columns>
<RowStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" />
</asp:GridView>

后台代码这样写:

//Gridview自动生成序号
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex > -1)
{
e.Row.Cells[
0].Text = Convert.ToString(e.Row.RowIndex + 1);
}
}

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载