How to Add Auto Increment Column to DataSet

Hello, below was the method to add Auto Increment Column to DataSet

public static DataSet GetDataTableAutoIncrement ( DataTable dataTableObject, string columnName )
DataTable dataTable = new DataTable ( dataTableObject.TableName );
DataColumn dataColumn = new DataColumn ( columnName, Type.GetType ( "System.Int32" ) );
dataColumn.AutoIncrement = true;
dataColumn.AutoIncrementSeed = dataColumn.AutoIncrementStep = 1;
dataTable.Columns.Add ( dataColumn );
DataTableReader dataTableReader = new DataTableReader ( dataTableObject );
dataTable.Load ( dataTableReader );
DataSet dataSet = new DataSet ();
dataSet.Tables.Add ( dataTable );
return dataSet;

Siva Prasad Palaparthi
Software Engineer.


Author: Joshua Riddle20 Oct 2010 Member Level: Bronze   Points : 1

Excellent information...This was exactly what i was looking for. It allowed be to take a table from a poorly designed DB with no primary key and add one dynamically. Making my coding job a lot easier.

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: