网络编程 
首页 > 网络编程 > 浏览文章

GridView自动增加序号(三种实现方式)

(编辑:jimmy 日期: 2025/12/27 浏览:3 次 )
第一种方式,直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了.
复制代码 代码如下:
<asp:TemplateField HeaderText="序号" InsertVisible="False">
<ItemTemplate>
<%#Container.DataItemIndex+1%>
</ItemTemplate>
</asp:TemplateField>

第二种方式分页时进行了计算,这样会累计向下加.
复制代码 代码如下:
<asp:TemplateField HeaderText="序号" InsertVisible="False">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center"/>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# this.GridView1.PageIndex * this.GridView1.PageSize + this.GridView1.Rows.Count + 1%>' />
</ItemTemplate>
</asp:TemplateField>

还有一种方式放在cs代码中,和第二种相似.
复制代码 代码如下:
<asp:BoundField HeaderText="序号" ></asp:BoundField>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{
int indexID = this.GridView1.PageIndex * this.myGridView.PageSize + e.Row.RowIndex + 1;
e.Row.Cells[0].Text = indexID.ToString();
}
}
上一篇:关于WPF使用MultiConverter控制Button状态的详细介绍
下一篇:asp.net中Word转Html的办法(不需要WORD组件)
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap