AutoIncrementSeed

vellaima

Well-known member
Joined
Jan 29, 2003
Messages
109
I have created a DataTable with a Serial No Column. In this Column I am using AutoIncrementSeed. When I delete a row in the DataTable I would like to re-number it again.

DataTable : MyTable
S.No Items
1 Item A
2 Item B
3 Item C

In the above scenario, if i delete Item B it should be re-numbered as follows.

DataTable : MyTable
S.No Items
1 Item A
2 Item C.

Is it possible in VB.NET? Could you please help me out?
 
Sorry - I shouldve been more informative. You cant to it with an AutoIncrementSeed column. Youll have to have a basic numeric column to hold the sequence number. Then, when you want to re-set the sequence youll have to open a recordset and run through each record assigning a new sequence number.

I would still like to know why? There must be a better way to do what you require.
 
Its just occurred to me that you could use a query - if your serial number is a numeric (sorry - Im a bit slow today :)).

If you delete item with serial number X then run a query to modify all items with a serial number >X and set it to the current value minus 1. Something like this, assuming youve just deleted item with serial number 5:


UPDATE mytable SET mycolumn = mycolumn-1 WHERE mycolumn > 5
 
If you plan on saving this data to a DataBase as an Identity column you shouldnt have to worry - the number in the DataBase will override whatever is in the DataSet. So even though you show "1" and "3" the database may substitute "467" and "468".

If you are using the AutoIncrement as a pure sequence for the current data only, then you shouldnt use AutoIncrement but use TechnoTones advice and make it a regular number. Youll have to manually change the number as rows are added/deleted. During a Delete of a DataRow, youll have to grab the current value, loop through all rows that are higher numbered and decrement their number by one. Well, thats one way to do it anyway. The point is that youll have to do it manually :)

Im not sure what TechnoTone means with the "UPDATE myTable..." since I dont know how youd execute this "query" against a DataSet. If you had already saved it to a database, maybe - but Id think youd want to change it before it got saved to the DB.

-Nerseus
 
I am doing as TechoTone has suggested. Looping thro manually using regular number. Thanks for the help provided.
 
Back
Top