none
How to Auto generate Serial number in GridView in Windows Application

Answers

  • If you are binding a DataTable to the DataGridView, then you can add a new DataColumn with its AutoIncrement property set to true.

          DataColumn dcAuto = new DataColumn();
          dcAuto.AutoIncrement = true;
          dcAuto.AutoIncrementSeed = 1;
          dt.Columns.Add(dcAuto);
    
    Where dt is the DataTable bound to your DataGridView


    Please mark this as answer or vote as helpful if it solved your problem
    Wednesday, June 15, 2011 8:22 AM

All replies

  • Do you mean the DataGridView control in Windows Forms or GridView in asp.net?

    Also, do you mean the Row Index of the selected row?

    If you do mean DataGridView then refer:  http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx

    If you mean GridView then refer: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx

    Hint: There is an Index property for each DataGridViewRow and the SelectedRows returns a DataGridViewRowCollection


    Please mark this as answer or vote as helpful if it solved your problem
    • Proposed as answer by Sivapragasam Tuesday, June 14, 2011 8:30 AM
    • Unproposed as answer by Sivapragasam Tuesday, June 14, 2011 8:54 AM
    Tuesday, June 14, 2011 8:09 AM
  • Hi Midhunkumarsingh,

                                    Do you want to show the row number in a separate column or row index value in a column.

    Tuesday, June 14, 2011 8:58 AM
  • I need to auto generate Serial number in seperate column


    Alluru.Midhun Kumar Singh




    Tuesday, June 14, 2011 9:59 AM
  • If you are binding a DataTable to the DataGridView, then you can add a new DataColumn with its AutoIncrement property set to true.

          DataColumn dcAuto = new DataColumn();
          dcAuto.AutoIncrement = true;
          dcAuto.AutoIncrementSeed = 1;
          dt.Columns.Add(dcAuto);
    
    Where dt is the DataTable bound to your DataGridView


    Please mark this as answer or vote as helpful if it solved your problem
    Wednesday, June 15, 2011 8:22 AM
  •  private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
            {
                dataGridView1.Rows[e.RowIndex].Cells[0].Value = e.RowIndex + 1;
            }
    Sunday, July 01, 2012 6:44 AM
  • It is very simple . use this code and for detail click on read more link

    Private Sub DataGridView1_CellValueNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValueEventArgs) Handles DataGridView1.CellValueNeeded
            If e.RowIndex >= 0 AndAlso e.ColumnIndex = Me.sno.Index Then
                e.Value = e.RowIndex + 1
            End If
        End Sub


    Read more: 
    http://ram-a-singh.blogspot.in/2011/09/how-to-create-serial-number-in-datagrid.html



    • Edited by rasingh1 Saturday, August 24, 2013 4:25 PM link change
    Thursday, February 28, 2013 11:08 AM