none
How to display row numbers in GridView,urgently required

    Question

  • Hi,

    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.


    Wednesday, December 06, 2006 3:07 PM

Answers

  • Try using this...

    it worked for me...

     

    <asp:TemplateField>
      <ItemTemplate>
        <%# Container.DataItemIndex + 1 %>
      </ItemTemplate>
    </asp:TemplateField>

    Tuesday, July 17, 2007 6:38 AM
  • 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.

    Thursday, December 07, 2006 3:49 PM

All replies

  • If 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.
    Wednesday, December 06, 2006 10:16 PM
  • 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 7:11 AM
  • 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.

    Thursday, December 07, 2006 3:49 PM
  • Try using this...

    it worked for me...

     

    <asp:TemplateField>
      <ItemTemplate>
        <%# Container.DataItemIndex + 1 %>
      </ItemTemplate>
    </asp:TemplateField>

    Tuesday, July 17, 2007 6:38 AM
  • Very elegant -- thanks for posting this.

    Thursday, November 15, 2007 5:04 AM
  • Awesome! Thank You Venus Lazarus 

    Thursday, December 06, 2007 4:41 PM
  • Thanks a ton venus, just what i was looking for. very helpful
    Friday, January 18, 2008 8:40 PM

  • i dun know where to put this code and what each means
    please guide me,
    thanks
    Sunday, March 09, 2008 2:12 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>



    Wednesday, April 23, 2008 1:45 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, September 04, 2008 9:57 PM
  • Thank you very much.

    <asp:TemplateField>
      <ItemTemplate>
        <%# Container.DataItemIndex + 1 %>
      </ItemTemplate>
    </asp:TemplateField>

    It worked for me too...

    Thursday, July 28, 2011 12:38 AM
  • 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
    Tuesday, October 18, 2011 1:55 PM
  • 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 %>

    Saturday, January 14, 2012 7:58 AM
  • It was very useful, Thanks Venus
    Wednesday, July 11, 2012 6:47 PM
  • Well done, thanks!


    The Web Guy

    Thursday, August 01, 2013 2:16 PM
  • Hello,

    For a Windows forms DataGridView we can write row number in the RowHeader of each row. The example below uses a DataTable as the DataGridView source and keeps the row number after sorting because we are setting it in CellFormatting event using e.RowIndex.

    Public Class MarkerForm
        Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
            If Not DataGridView1.Rows(e.RowIndex).IsNewRow Then
                DataGridView1.Rows(e.RowIndex).HeaderCell.Value = e.RowIndex.ToString
            End If
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            DataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToFirstHeader
            DataGridView1.EnableHeadersVisualStyles = False
            Dim dt As New DataTable With {.TableName = "MyTable"}
            dt.Columns.AddRange(New DataColumn() _
                                {New DataColumn("Column1", GetType(System.String)), _
                                 New DataColumn("Column2", GetType(System.String)), _
                                 New DataColumn("Column3", GetType(System.String))})
            dt.Rows.Add(New Object() {"A", "1", "1"})
            dt.Rows.Add(New Object() {"B", "2", "2"})
            dt.Rows.Add(New Object() {"C", "3", "3"})
            dt.Rows.Add(New Object() {"D", "4", "4"})
            dt.Rows.Add(New Object() {"E", "5", "5"})
            dt.Rows.Add(New Object() {"F", "6", "6"})
            DataGridView1.DataSource = dt
        End Sub
    End Class

    Unbound example

    Public Class MarkerForm
        Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
            If Not DataGridView1.Rows(e.RowIndex).IsNewRow Then
                DataGridView1.Rows(e.RowIndex).HeaderCell.Value = e.RowIndex.ToString
            End If
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            DataGridView1.ColumnCount = 3
            DataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToFirstHeader
            DataGridView1.EnableHeadersVisualStyles = False
            DataGridView1.Rows.Add(New Object() {"A", "1", "1"})
            DataGridView1.Rows.Add(New Object() {"B", "2", "2"})
            DataGridView1.Rows.Add(New Object() {"C", "3", "3"})
            DataGridView1.Rows.Add(New Object() {"D", "4", "4"})
            DataGridView1.Rows.Add(New Object() {"E", "5", "5"})
            DataGridView1.Rows.Add(New Object() {"F", "6", "6"})
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Sunday, August 04, 2013 6:05 PM
    Moderator