第一种方式,直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了.
<asp:TemplateField HeaderText="序号"
InsertVisible="False">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="5%" />
<ItemTemplate>
<% #Container.DataItemIndex+1%>
</ItemTemplate>
</asp:TemplateField>
第二种方式分页时进行了计算,这样会累计向下加.
<asp:TemplateField HeaderText="序号"
InsertVisible="False">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="5%" />
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# this.MyListGridView.PageIndex * this.MyListGridView.PageSize + this.MyListGridView.Rows.Count + 1%>'/>
</ItemTemplate>
</asp:TemplateField>
还有一种方式放在cs代码中,和第二种相似.
<asp:BoundField HeaderText="序号"
>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="5%" />
</asp:BoundField>
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{
int indexID = this.myGridView.PageIndex * this.myGridView.PageSize + e.Row.RowIndex + 1;
e.Row.Cells[0].Text = indexID.ToString();
}
}
gridview如何添加一个自增的列
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
在昨天作业的基础上添加 :删除按钮,修改并保存按钮 和 添加按钮。完成这些按钮所对应的功能(XmlDocument)。
yang_qiao
2018-08-27 19:28:13
MultiView和View
yang_qiao
2018-08-27 19:28:13
写student类中的select,delete,update,insert方法并在objectDataSource中应用(分别写两个,一个返回datatable,一个使用list)
yang_qiao
2018-08-27 19:28:12
XML --- 使用XlmReader读books.xml中有几本书。使用XlmWriter写NewBooks.xml文件。
yang_qiao
2018-08-27 19:28:12
计算机基础及ASP动态网页设计——第一章 计算机基础知识
bai79425120
2018-08-27 17:50:26
数据库调出显示为下拉列表
kliffy
2018-08-27 17:30:43
解决(Microsoft JET Database Engine (0x80040E09)/不能更新。数据库或对象为只读。)
hellohuan
2018-08-27 16:52:29
.net 的TreeView控件
ikendream
2018-08-27 16:11:06
JS-asp:RadioButtonList
xu_shuoshuo
2018-08-27 15:52:45
ASP水晶报表
xu_shuoshuo
2018-08-27 15:52:45