DataGrid自动增加序号(三种实现方式)
方法一:,直接在Aspx页面DataGrid模板列中.
缺点是到第二页分页时又重新开始了.
<asp:TemplateColumn HeaderText="序号" FooterText=" " HeaderStyle-Height="20px" FooterStyle-Height="20px" HeaderStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center" >
<ItemTemplate>
<%#Container.DataSetIndex+1 %>
</ItemTemplate>
<HeaderStyle Width="50px" />
<ItemStyle HorizontalAlign="Center" /></asp:TemplateColumn>
方法二:分页时进行了页面计算,这样会累计向下加.
<asp:TemplateColumn HeaderText="序号" FooterText=" " HeaderStyle-Height="20px" FooterStyle-Height="20px" HeaderStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:Label ID="id" runat="server" Text='<%# this.dgGift.CurrentPageIndex*this.dgGift.PageSize+this.dgGift.Items.Count+1 %>' />
</ItemTemplate>
<HeaderStyle Width="50px" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateColumn>
方法三:放在cs代码中,和第二种相似.
<asp:TemplateColumn HeaderText="序号" FooterText=" " HeaderStyle-Height="20px" FooterStyle-Height="20px" HeaderStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center" > <HeaderStyle Width="50px" />
<ItemStyle HorizontalAlign="Center" />
<asp:Label ID="id" runat="server" />
</asp:TemplateColumn>
protected void DataGrid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (this.DataGrid1.Items.Count != 0)
{
int indexID = this.DataGrid1.CurrentPageIndex*this.DataGrid1.PageSize+this.DataGrid1.Items.Count+1;
(this.DataGrid1.Items[i].FindControl("id") as Label).Text = indexID.ToString();
}
}
GridView自动增加序号(三种实现方式)
方法一:,直接在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();
}
}