다음을 통해 공유


ASP.NET: How to insert Serial Numbers in repeater and gridview

We often need to put serial number or row number while listing data into a gridview or repeater control.

There are many methods to do this. We can do this either using C# code or directly writing in the stored procedure for retrieving the data.

By using the C# way, when using the Gridview control with paging option set,

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

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

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

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

The above 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