blog

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

Add identity column in Datatable in C#

AVADHESH PATEL16137 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.


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

Comments

Liked By