GridView automatically increases the serial number of in three ways

  • 2020-06-03 06:14:33
  • OfStack

The first is directly in the Aspx page GridView template column. This has the disadvantage of starting again by page 2.
 
<asp:TemplateField HeaderText=" The serial number " InsertVisible="False"> 
<ItemTemplate> 
<%#Container.DataItemIndex+1%> 
</ItemTemplate> 
</asp:TemplateField> 

The second method is calculated when paging, which adds up downward.
 
<asp:TemplateField HeaderText=" The serial number " 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> 

There is another way to put it in the cs code, similar to the second.
 
<asp:BoundField HeaderText=" The serial number " ></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(); 
} 
} 

Related articles: