Reset Auto Increment to DataTable

I populate a DataSet using the DataAdapter into a SQL CE database . Data is mapped to a DataGrid that is bound to a DataSet DataTable. I have an auto-increment identifier field (or in an SQLCE called PRIMARY KEY IDENTITY ) in my DataSource; accordingly, I also set the auto-increment identifier column in my DataTable .

 /* when DataTable is first populated, start counting from Rows.Count */ /* if empty, starts with 1 */ dt.Column["id"].AutoIncrementSeed = dt.Rows.Count + 1; 

The problem occurs when I clear my DataTable. I want the reset auto-increment counter to return to 1, but could not, I tried the following:

 /* clearing and disposing DataTable, DataSet, DataAdaptor does not reset the counter */ dt.Clear(); dt.Dispose(); ds.Clear(); ds.Dispose() da.Dispose() /* manually re-setting the AutoIncrementSeed also does not reset the counter */ dt.Column["id"].AutoIncrementSeed = 1; 

He just left with the counter turned off in front of Clear() . How can I reset auto-increment in a DataTable?

+4
source share
2 answers

Use it as

 dt.Clear(); dt.Column["id"].AutoIncrementStep = -1; dt.Column["id"].AutoIncrementSeed = -1; dt.Column["id"].AutoIncrementStep = 1; dt.Column["id"].AutoIncrementSeed = 1; 

Check out the link below for more

http://www.eggheadcafe.com/community/aspnet/10/25407/autoincrementseed.aspx

+5
source

I simplified the code for this version:

 dt.Column["id"].AutoIncrement = True dt.Column["id"].AutoIncrementSeed = 0 dt.Column["id"].AutoIncrementStep = -1 dt.Column["id"].AutoIncrementSeed = -1 

This is better for me, due to -1 values โ€‹โ€‹of seed and step. I think the implementation of the AutoIncrementSeed property is as follows:

 if (_AutoIncrementSeed <> value) _InternalAutoIncrementSeed = value _AutoIncrementSeed = value 

and _InternalAutoIncrementSeed is used in the NewRow function.

+1
source

All Articles