blog

home / developersection / blogs / add identity column in datatable in c#

Add identity column in Datatable in C#

AVADHESH PATEL 16733 01-Aug-2012

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.


c# c# 
Updated 18-Sep-2014

Avadhesh Kumar Patel District Project Manager - Aligarh 14 months work experience in Panchayati Raj Department Sector as District Project Manager & 12 months work experience in IT Sector as Software Engineer. :-)

Leave Comment

2 Comments

Comments

Liked By