Showing row number in Gridview
Posted on 2/6/2008 8:39:37 AM
in #ASP.NET 2.X
Hi,
A few days ago some one asked me this in a mail. After thinking on this a bit I decided to make a post as I thought this might be useful to many. Many a times while using a Gridview or a Datagrid we want to show a row number along with the Row, so that its easy for the user. The row number should always start from 1 no matter what the sorting is.
This can be done in many ways. We can fetch a record from the database itself using the identity column. But this small problem does not needs such a complex solution. It can easily be solved in the Gridview or Datagrid itself. What we need to do is add a new template column to the Gridview (I ma using Gridview’s example to do this same cane be done with Datagrid, datalist etc.). And in the template column add a new label. Now bind the label in the rowdatabound event. Here is a code snippet for the same in the rowdatabound event
if (e.Row.RowType == DataControlRowType.DataRow) { Label lableIndex; int i = e.Row.RowIndex + 1; lableIndex = e.Row.FindControl("lableIndex "); lableIndex.Text = i.ToString(); }
That’s all, this will show a running number of the row no matter what the sorting is in the Gridview.
Vikram
|
Posted on 4/1/2010 7:01:41 PM
It's quite useful. I didn't see "e.Row.RowType == DataControlRowType.DataRow" before. But you can't apply it if Grid uses paging. Need more enhancement.
|