How to display row numbers in GridView,urgently required
-
Wednesday, December 06, 2006 3:07 PMHi,
I need this very badly.
I am using a datagridview for which the datasource is a dataview. This dataview is assigned with a datatable from database.
I am able to display the row numbers for the gridview by adding new column like shown below:
int rowcount = table1.Rows.Count;
table1.Columns.Add(new DataColumn("SNO", typeof(int)));
for (int i = 1; i <= rowcount; i++)
{
table1.Rows[i - 1]["SNO"] = i;
}
But main problem arised when i sort any another colum , the "SNO" also gets sorted with row numbers not in proper order. Like if I sort by name then row numbers are displyed like 3,2,1 ..
Even when i use Dataview's row filter, only few columns are displayed with previous rownumbers. Suppose, after row filter we are left with only last two columns, then the Row numbers are displayed as 2,3 but '1' is missing because it was filtered in rowfilter.
But I want 1,2 to be displayed there.
Please can any one help. Thanks in Advance,
Sukanya.
All Replies
-
Wednesday, December 06, 2006 10:16 PMIf all you want is to display the rank of each row in the current gridview, then you should be able to modify the data binding to hook to some rank-generating code instead of trying to put the data into the dataset.
-
Thursday, December 07, 2006 7:11 AM
Hi,
If you want to show the rank in the current data table, you can add a public property to your datarow class that returns the rank and you can then bind this property to the column of the gridview.
Regards,
Charles -
Thursday, December 07, 2006 3:49 PM
You can display the row numbers like shown below:
//To add First column as rownumber
DataGridViewTextBoxCell newCell = new DataGridViewTextBoxCell();
DataGridViewColumn SNOCol = new DataGridViewColumn(newCell);
SNOCol.DisplayIndex = 0;
SNOCol.HeaderText = "SNO";
SNOCol.Name = "SNO";
DataGridView1.Columns.Add(SNOCol);
int cnt = DataGridView1.Rows.Count;
int i;
for (i = 1; i <= cnt; i++)
{
DataGridView1.Rows[i - 1].Cells["SNO"].Value = i;
}
Now during sorting or filtering, just remove the cell from datagridview and again add it. -
Tuesday, July 17, 2007 6:38 AM
Try using this...
it worked for me...
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField> -
Thursday, November 15, 2007 5:04 AM
Very elegant -- thanks for posting this.
-
Thursday, December 06, 2007 4:41 PM
Awesome! Thank You Venus Lazarus
-
Friday, January 18, 2008 8:40 PMThanks a ton venus, just what i was looking for. very helpful
-
Sunday, March 09, 2008 2:12 PM
i dun know where to put this code and what each means
please guide me,
thanks -
Wednesday, April 23, 2008 1:45 PMThanks for a good idea, Venus!
I've upgraded your variant to be used with paging. :-)
Code Snippet<asp:TemplateField HeaderText="#">
<ItemTemplate>
<%# (((GridView)((GridViewRow)Container).Parent.Parent).PageIndex) * ((GridView)((GridViewRow)Container).Parent.Parent).PageSize + Container.DataItemIndex + 1 %>.
</ItemTemplate>
</asp:TemplateField> -
Thursday, September 04, 2008 9:57 PM
Same problem, but cannot use above solution since the grid is being created dynamically. I need to add the row numbers via VB code. Can someone provide the VB code equivalent to the above template solution for showing row numbers in a DataGrid?
(ps - the row numbers cannot be limited to a default of 1-n since I'm paging results on the back end, it has to show the current start number for the current sort order and paged result).
-
Thursday, July 28, 2011 12:38 AM
Thank you very much.
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>It worked for me too...
-
Tuesday, October 18, 2011 1:55 PM
Thanks for a good idea, Venus!
I've upgraded your variant to be used with paging. :-)
Code Snippet<asp:TemplateField HeaderText="#">
<ItemTemplate>
<%# (((GridView)((GridViewRow)Container).Parent.Parent).PageIndex) * ((GridView)((GridViewRow)Container).Parent.Parent).PageSize + Container.DataItemIndex + 1 %>.
</ItemTemplate>
</asp:TemplateField>
Can be simplified :)
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<%# {GridViewID}.PageIndex * {GridViewID}.PageSize + Container.DataItemIndex + 1 %>.
</ItemTemplate>
</asp:TemplateField>- Proposed As Answer by denisk1981 Tuesday, October 18, 2011 1:55 PM
-
Saturday, January 14, 2012 7:58 AM
Hi Denis
What you have contributed is good but there's a mistake.
The dataitemindex doesnt start from 0 for every page of data. It actually continues from the last page i.e if your page size is 20, the dataitemindex for the first row of next page will be 21(adding 1 to avoid starting S. No. from 0) . The expression you gave would result in wrong S.No from the second page onward. So, if you need to add a S. No. column that continues the count from the last page, the expression would simply be:
<%# Container.DataItemIndex+1 %>
In other case if you wish the S.No to start from 1 for every page, the expression would be:
<%# ((GridViewRow) Container).RowIndex+1 %>
-
Wednesday, July 11, 2012 6:47 PMIt was very useful, Thanks Venus


