469,336 Members | 6,062 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

VB.NET: DataTable Cloning and Seeds with .NET 2

Hi,

When cloning a DataTable, it appears that seed counts are also copied to the new table e.g. dtbSource has 50 rows, clone dtbSource to dtbClone and then add the first new row to dtbClone ... this new row will have an "ID" of 51 rather than 1. Setting AutoIncrementSeed on dtbClone has no effect whatsoever. In actual fact I have tried multiple variations of AutoIncrementSeed and AutoIncrementStep including:

Expand|Select|Wrap|Line Numbers
  1. dtbClone.Columns("ID").AutoIncrementStep = -1
  2. dtbClone.Columns("ID").AutoIncrementSeed = -1
  3. dtbClone.Columns("ID").AutoIncrementStep = 1
  4. dtbClone.Columns("ID").AutoIncrementSeed = 1
... but nothing seems to have the desired effect. All I want is dtbSource to carry on from the point of cloning e.g. 51, 52 etc. and dtbClone to start from 1.

Thanks in advance for any insight,

Rod.
Dec 11 '07 #1
6 1997
Plater
7,872 Expert 4TB
Clone claims to copy over all formatting data too right, that's what is causing the problem?
Could you get away with changing the autoincrement stuff on the source, make the clone, then turn the auto increment stuff back ON on the source?
Dec 11 '07 #2
Thanks for the quick response. That certainly is an interesting approach, and not one that I had thought of. I can give it a try and get back to you.

Thanks,

Rod.
Dec 11 '07 #3
Bad news ... I tried the following rough and ready code:

Expand|Select|Wrap|Line Numbers
  1. dtbSource.Columns("ID").AutoIncrementSeed = 1
  2. dtbSource.AcceptChanges()
  3.  
  4. dtbClone = dtbSource.Clone()
  5.  
  6. dtbSource.Columns("ID").AutoIncrementSeed = 1000
  7. dtbSource.AcceptChanges()
... and although dtbSource then started counting from 1000, dtbClone continued counting from the original row count of dtbSource rather than 1. Something tells me you can't wind the seed back when the Unique property is set to True, you can only send it forward.

Any other thoughts?

Thanks,

Rod.
Dec 11 '07 #4
Plater
7,872 Expert 4TB
Hmm, what are you going to do with the clone?
Could you maybe delete/recreate the column? That might make the counter start at one.
Dec 11 '07 #5
demaus
6
don't know if this will work but you could give it a try

Expand|Select|Wrap|Line Numbers
  1.  
  2. dtClone = dtSource.Clone()
  3. dtClone.Clear()
  4.  
haven't tried this in code myself, I know that .Clear() will remove data and maybe it will reset the increment?.
Dec 11 '07 #6
Good call Plater ... I am beginning to think that deleting and then recreating the column might be the simplest way out of this predicament. I will try that next.

Incidentally, the purpose of the Clone is to act as multi-row Cut, Copy & Paste clipboard for the Source. I'm not sure whether that changes anything, but it seems to be the most efficient approach.

Many thanks for the suggestion demaus. I omitted to mention in my first post that I had also tried the Clear method, but to no avail, but thanks anyway :-)

Rod.
Dec 11 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by AVL | last post: by
4 posts views Thread by hharry | last post: by
6 posts views Thread by Intiha | last post: by
7 posts views Thread by Wayne Gibson | last post: by
1 post views Thread by Dan Dorey | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.