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.

  Modified On Sep-18-2014 01:24:03 PM

Leave Comment