Home > DeveloperSection > Blogs > Add identity column in Datatable in C#

Add identity column in Datatable in C#


C# C# 
Ratings:
0 Comment(s)
 10103  View(s)
Rate this:

Add identity column in Datatable in C#

Below code which will add an Identity column in DataTable.

DataTable dt = new DataTable("Information");

        DataColumn dcID = new DataColumn("ID", typeof(int));

        dcID.AutoIncrement = true;

        dcID.AutoIncrementSeed = 1;

        dcID.AutoIncrementStep = 1;

        // Add identity column to datatable

        dt.Columns.Add(dcID);

        dt.Columns.Add("FirstName"); // Other columns

        dt.Columns.Add("LastName");

        dt.Columns.Add("Age");

        dt.Columns.Add("Gender");

Adding new row in Datatable

dr["FirstName"] = "Avadhesh";

        dr["LastName"] = "Patel";

        dr["Age"] = 24;

        dr["Gender"] = "Male";

        dt.Rows.Add(dr);

The above code will add a new row with the ID = 1.


Don't want to miss updates? Please click the below button!

Follow MindStick