我們經(jīng)常會(huì)碰到GridView需要添加一個(gè)序號(hào)列的情況,那么你知道GridView如何才能自動(dòng)增加序號(hào)嗎?下面我們就一起探討GridView自動(dòng)增加序號(hào)的實(shí)現(xiàn)方式。
第一種方式,直接在Aspx頁面GridView模板列中.這種的缺點(diǎn)是到第二頁分頁時(shí)又重新開始了.
復(fù)制代碼 代碼如下:
<asp:TemplateField HeaderText="序號(hào)" InsertVisible="False">
<ItemTemplate>
<%#Container.DataItemIndex+1%>
</ItemTemplate>
</asp:TemplateField>
第二種方式分頁時(shí)進(jìn)行了計(jì)算,這樣會(huì)累計(jì)向下加.
?
復(fù)制代碼 代碼如下:
?
<asp:TemplateField HeaderText="序號(hào)" 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代碼中,和第二種相似.
?
復(fù)制代碼 代碼如下:
?
<asp:BoundField HeaderText="序號(hào)" ></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();
}
}
上文就是GridView自動(dòng)增加序號(hào)的實(shí)現(xiàn)方式,?需要的朋友趕緊動(dòng)手嘗試一下吧,這在我們?nèi)蘸笫褂?span style="color:#002FD9">ASP.NET