How to create auto number, Serial number, Row number in DataGridView in C# Windows application

Display auto number, serial number, row number in the data grid view is very simply; just you have to create one new column for the date grid, after created the new column make that column to read only. And enable Data Grid View VirtualMode property to True, and assign e.rowindex under CellValueNeeded events. see the step by step screens below. The CellValueNeeded events occurs when the VirtualMode property of the DataGridView control is true and the DataGridView requires a value for a cell in order to format and display the cell.

Step 1 : Create one new column for the Data Grid View and make that column to read only.

Step 2 : Enable Data Grid View VirtualMode property to True

Step 3 : Assign e.rowindex under CellValueNeeded events.

That’s it, after you completed all above 3 steps, run the program as-usual, you can see the auto number in the first column.  Please post your comments and new ideas.

18 Responses to“How to create auto number, Serial number, Row number in DataGridView in C# Windows application”

  1. Rajalingam
    May 7, 2012 at 7:08 am #

    Hi friend,

    Its very useful to me…..


  2. Muhammad Tahir Khan
    January 8, 2013 at 8:01 pm #

    Thanks its works fine

  3. babu
    January 24, 2013 at 12:10 pm #

    When i set virtual property is true then data is not loading into the datagridview and entered data disappeared when go to next row in datagridview.

  4. mangesh
    March 7, 2013 at 12:05 pm #

    Above Code would not Perform In our Project
    I m using Visual Studio 2008
    plz tell me how to do it or explain it

  5. adnan umar
    April 4, 2013 at 7:12 am #

    Thanks bother nice method its working good…

  6. Selva
    May 10, 2013 at 5:38 am #

    i have a problem to using this…. it displays only one rows of serial number.i want to display the sequence serial no for all columns from database….. so i need ur help as soon as possible…..

  7. mehdi
    September 19, 2013 at 7:16 am #

    thanks , thanks , thanks , thanks :-*
    Its Very Very UseFul To Me :-*

  8. Udam
    December 14, 2013 at 7:03 am #

    Thank you so much it works !!! :DDDD

  9. Ahmed Mansour
    December 25, 2013 at 12:35 pm #

    //1-add new column to dataGridView1 call it ser or your Name
    //2-set it’s DataPropertyName To None “Very Important”
    //3-Set HeaderText to (“Your Name To shown In Header”)
    //4-Set frozen To True
    //5-in DefultCellStyle Do Thise
    // A-Set BackColor To AnyColor you Want
    // b-Set Null Value to 0 or 1 as you prefer
    // b-Set Alignment To middleCenter
    //6- call Method MakeSerieal() everyTime You Want To fill dataGridView1
    //7- call Method MakeSerieal() In ColumnHeaderMouseClick (“It help if you Want To Reorder dataGridView1”)
    private void MakeSerieal()

    if (dataGridView1.Rows.Count > 1)
    for (int i = 0; i com

  10. March 5, 2014 at 12:05 pm #

    Thank you,its work fine. !!!!!!!

  11. Rajeev
    June 26, 2014 at 2:17 am #

    “Thanks bro “

  12. July 17, 2014 at 2:48 am #

    Thanks forr finally writing about > How to create auto number,
    Serial number, Row number in DataGridView in C# Windows application < Liked it!

  13. Akwin
    December 5, 2014 at 6:01 am #

    thank u so much its very useful

  14. Sanjay
    December 8, 2014 at 10:13 pm #

    Really i was search 1 year, but now only i got ur idea…Very thank you sir…U r the Genius….
    Please sir i wana contact u,this my mail id

  15. December 19, 2014 at 3:52 pm #

    This code help me alot while I faced a problem. thanks

  16. Nitesh Kumar
    April 22, 2015 at 12:28 pm #

    Thanks it is working good…………..

  17. Jayshree Derle
    May 10, 2015 at 2:54 pm #

    thanx aaa lot…!!!..

  18. Mazhar
    July 27, 2015 at 12:20 pm #

    e.value is not working in my project. It is saying that ‘System.Windows.forms.datagridvieweventcellargs’ does not contain definition for ‘Value. Any help?’

Leave a Reply

Your email address will not be published. Required fields are marked *


Proudly powered by WordPress   Premium Style Theme by