In this article I will explain with an example, how to add (show) Auto Increment Number in First column of DataGridView in Windows Forms Application using C# and VB.Net.
The Auto Increment Number will be shown in DataGridView with the help of DataTable in Windows Forms (WinForms) Application using C# and VB.Net.
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
Form Design
The Form consists of a DataGridView control.
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Add (Show) Auto Increment Number in DataGridView in Windows Forms Application
Inside the Form Load event, the AutoGenerateColumns property of DataGridView is set to False and manually columns are added to the DataGridView.
The GetData function is used as source of data for the DataGridView.
Inside the GetData function, first the records are fetched from table using SqlDataReader and then a DataTable is created with an AutoIncrement column.
Then one by one records fetched by SqlDataReader are inserted into the DataTable. Finally the DataTable is returned which is used to populate the DataGridView.
C#
private void Form1_Load(object sender, EventArgs e)
{
//Set AutoGenerateColumns False.
dataGridView1.AutoGenerateColumns = false;
//Set Columns Count.
dataGridView1.ColumnCount = 4;
//Add Columns.
dataGridView1.Columns[0].Name = "SerialNo";
dataGridView1.Columns[0].HeaderText = "Serial No.";
dataGridView1.Columns[0].DataPropertyName = "SerialNo";
dataGridView1.Columns[1].Name = "CustomerId";
dataGridView1.Columns[1].HeaderText = "Customer Id";
dataGridView1.Columns[1].DataPropertyName = "CustomerID";
dataGridView1.Columns[2].HeaderText = "Contact Name";
dataGridView1.Columns[2].Name = "ContactName";
dataGridView1.Columns[2].DataPropertyName = "ContactName";
dataGridView1.Columns[3].Name = "Country";
dataGridView1.Columns[3].HeaderText = "Country";
dataGridView1.Columns[3].DataPropertyName = "Country";
dataGridView1.DataSource = GetData("SELECT CustomerID, ContactName, Country FROM Customers");
}
private static DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass@123";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.Text;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
//Add columns to DataTable.
dt.Columns.AddRange(new DataColumn[4] {
new DataColumn("SerialNo"),
new DataColumn("CustomerID"),
new DataColumn("ContactName"),
new DataColumn("Country")
});
//Set AutoIncrement True for the First Column.
dt.Columns["SerialNo"].AutoIncrement = true;
//Set the Starting or Seed value.
dt.Columns["SerialNo"].AutoIncrementSeed = 1;
//Set the Increment value.
dt.Columns["SerialNo"].AutoIncrementStep = 1;
while (sdr.Read())
{
dt.Rows.Add(null, sdr["CustomerID"], sdr["ContactName"], sdr["Country"]);
}
}
con.Close();
}
}
return dt;
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
'Set AutoGenerateColumns False.
dataGridView1.AutoGenerateColumns = False
'Set Columns Count.
dataGridView1.ColumnCount = 4
'Add Columns.
dataGridView1.Columns(0).Name = "SerialNo"
dataGridView1.Columns(0).HeaderText = "Serial No."
dataGridView1.Columns(0).DataPropertyName = "SerialNo"
dataGridView1.Columns(1).Name = "CustomerId"
dataGridView1.Columns(1).HeaderText = "Customer Id"
dataGridView1.Columns(1).DataPropertyName = "CustomerID"
dataGridView1.Columns(2).HeaderText = "Contact Name"
dataGridView1.Columns(2).Name = "ContactName"
dataGridView1.Columns(2).DataPropertyName = "ContactName"
dataGridView1.Columns(3).Name = "Country"
dataGridView1.Columns(3).HeaderText = "Country"
dataGridView1.Columns(3).DataPropertyName = "Country"
dataGridView1.DataSource = GetData("SELECT CustomerID, ContactName, Country FROM Customers")
End Sub
Private Shared Function GetData(ByVal query As String) As DataTable
Dim dt As DataTable = New DataTable()
Dim constr As String = "Data Source=.\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass@123"
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.CommandType = CommandType.Text
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
'Add columns to DataTable.
dt.Columns.AddRange(New DataColumn(3) {New DataColumn("SerialNo"), _
New DataColumn("CustomerID"), _
New DataColumn("ContactName"), _
New DataColumn("Country")})
'Set AutoIncrement True for the First Column.
dt.Columns("SerialNo").AutoIncrement = True
'Set the Starting or Seed value.
dt.Columns("SerialNo").AutoIncrementSeed = 1
'Set the Increment value.
dt.Columns("SerialNo").AutoIncrementStep = 1
While sdr.Read()
dt.Rows.Add(Nothing, sdr("CustomerID"), sdr("ContactName"), sdr("Country"))
End While
End Using
con.Close()
End Using
End Using
Return dt
End Function
Screenshot
Downloads