RSS

Reset AutoIncrement column in DataTable

08 Jun

I have a auto-numbering column “AUTONUMBER” in a DataTable which is only used for showing the query result count, it is not related to any column in the database table. The AutoIncrementSeed and AutoIncrementStep properties of the column were set to 1. Before each query the DataTable.Clear() is called. I expected after every query the column would have started at 1 but instead it was incremented from the last number. The solution is simply add 4 lines after DataTable.Clear(). I don’t know exactly what happens under these code though…

dataTable.Clear()
'Add these 4 lines will reset the AutoIncrement column
dataTable.Columns("AUTONUMBER").AutoIncrementStep = -1
dataTable.Columns("AUTONUMBER").AutoIncrementSeed = -1
dataTable.Columns("AUTONUMBER").AutoIncrementStep = 1
dataTable.Columns("AUTONUMBER").AutoIncrementSeed = 1

Reference:Reset AutoIncrement in DataTable

Advertisements
 
1 Comment

Posted by on June 8, 2012 in .NET, ADO.NET, VB

 

One response to “Reset AutoIncrement column in DataTable

  1. Dan

    November 5, 2012 at 4:15 PM

    Thank you! This is what I’m looking for!

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: