How to Insert Serial Numbers in Repeater and GridView in ASP.Net

We often need to put a serial number or a row number while listing the data into a GridView or a repeater control. There are many methods to do this. We can do this either by using a C# code or directly by writing in the stored procedure for retrieving the data.

By using the C# method: 

When using the GridView control with paging option set,

<%#(gvResult.PageIndex * gvResult.PageSize) + (gvResult.Rows.Count + 1)%>

Use this code then you will get the row number in the
GridView.

When using the Repeater control (remember repeater control has no paging option in the default, but we can give it with the paging option set).

<%#(((RepeaterItem)Container).ItemIndex+1).ToString()%>

The above mentioned code will do it for you.

The third option is directly selecting the row number from the stored procedure itself.

SELECT
ROW_NUMBER()  OVER
(ORDER
BY 
ColumnName1)
As SrNo, ColumnName1, 
ColumnName2 FROM  TableName

This will select the row number from the stored procedure by itself.