Tuesday, February 21, 2006

Create a simple Serial/row No column in Datagrid/DataList

A very simple way to create Serial/row No column in Datagrid/DataList

create a variable in the code behind


protected int i=1;

Now in the Datagrid/DataList add a TemplateColumn with data bound to the variable



<asp:TemplateColumn HeaderText="SL#">
<ItemTemplate>
<%# i++ %>
</ItemTemplate>
</asp:TemplateColumn>


This may not be usefull for item identification when used in sortable grid

4 comments:

manish said...

But prashant this doesnot work for a pageable datagrid/gridview control.

Prashant Shrivastava said...

Hey Prashant it works fine with datalist

thanks buddy :)

pradeep said...

Hi prashanth,
It works for Datlist, but the problem is,datlist is implemented with pagination,so in the next page the numbers are again displayed from 1.

Prashant said...

We should be able to initialize the i with page# * #of rows/page in the page index change event