How to Generate Serial Numbers in C# Windows Application DataGridView

This blog describes the generation of serial numbers in a C# Data Gridview Control Windows Application, which can be used for inventory counting.

First add the Data Gridview in the windows form, then add the Text Box Column and name it SNo.


This small piece of code is used to achieve this task.

Method for Generating Serial Numbers

private void LoadSerial()
{
    int i = 1;
    foreach (DataGridViewRow row in dataGridView1.Rows)
   {
         row.Cells["SNo"].Value = i; i++; }
   }
}
 
Call this method on the Load Event for starting

private void Form1_Load(object sender, EventArgs e)
{
    LoadSerial();
}

And then call this on CellEndEdit Event of Gridview

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    LoadSerial();
}

This will generate the number while writing on the GridView.

You can Download the Example and have a look into it.