How to add auto increment column in the DataTable?

 Posted by Raja on 6/7/2009 | Category: ADO.NET Interview questions | Views: 28252

To add auto increment column in the DataTable, we can set the AutoIncrement property of the DataColumn object as true and specify the seed value after adding that field into the DataTable object.

// create columns for the DataTable

DataTable dTable = new DataTable();
DataColumn auto = new DataColumn("AutoID", typeof(System.Int32));
// specify it as auto increment field
auto.AutoIncrement = true;
auto.AutoIncrementSeed = 1;
auto.ReadOnly = true;

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response