第一種方式,直接在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