none
how to clone datatable with primary key columns RRS feed

  • Question

  • hi dears

    i have a datatable and want to create another table same to that. but when i use clone method to copy schema, primary key columns does not copy to new table. is there any way to clone datatable schema with primary keys?

    dt2 = dt1.clone();

    dt2 does not have primary keys.

    Saturday, March 10, 2012 6:18 AM

Answers


  • It did work for me both in version 10SP1 and Beta 11. 

    Because I wanted to test beta11 VB I took only one language not yours, but that would not differ.  If you want this code in C# then replace the Dim for Var, in some places the parenthesis for square brackets [] (for arrays) and set the keys array like this DataColumn keys = {null,null}; Also end all lines with a semicolon ;


    Success
    Cor


    • Edited by Cor Ligthert Sunday, March 11, 2012 10:05 AM mistake array declaration C#
    • Marked as answer by Allen_MSDNModerator Friday, March 16, 2012 2:21 AM
    Sunday, March 11, 2012 9:54 AM
  • No it is not destroyed, 

    A datatable is a little bit strange class, it has two main collections in it which are depending to each other (this is in fact wrong in OO).

    If you do ToTable than a new DataTable is created. However, the columns property in the new table is than still pointing to the old Table.

    Therefore I recreated the keys new in the sample I've made for Steve yesterday (in C#) yesterday, it was by coincidence the same subject.

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/5d8a6731-33d2-40aa-9374-12098190bc03


    Success
    Cor


    Monday, March 12, 2012 8:06 AM

All replies


  • It did work for me both in version 10SP1 and Beta 11. 

    Because I wanted to test beta11 VB I took only one language not yours, but that would not differ.  If you want this code in C# then replace the Dim for Var, in some places the parenthesis for square brackets [] (for arrays) and set the keys array like this DataColumn keys = {null,null}; Also end all lines with a semicolon ;


    Success
    Cor


    • Edited by Cor Ligthert Sunday, March 11, 2012 10:05 AM mistake array declaration C#
    • Marked as answer by Allen_MSDNModerator Friday, March 16, 2012 2:21 AM
    Sunday, March 11, 2012 9:54 AM
  • As Cor says, it does work in VS2010. I also tested it in VS2008 SP1 ... it works there also. I do not have VS2005 (or 03) installed on this particular machine, so I can't test it for any other versions.

    You must be using an earlier version ... what version of Visual Studio are you using?

    If earlier versions do not support this, you might want to try doing the following:

    DataColumn[] keys = dt1.PrimaryKey.Clone() as DataColumn[];
    DataColumn[] keysNew = new DataColumn[keys.Length];
    DataTable dt2 = dt1.Clone();
    for (int i = 0; i < keys.Length; i++)
    {
        keysNew[i] = dt2.Tables[1].Columns[keys[i].ColumnName];
    }
    dt2.PrimaryKey = keysNew;


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Sunday, March 11, 2012 10:56 PM

  • It did work for me both in version 10SP1 and Beta 11. 

    Because I wanted to test beta11 VB I took only one language not yours, but that would not differ.  If you want this code in C# then replace the Dim for Var, in some places the parenthesis for square brackets [] (for arrays) and set the keys array like this DataColumn keys = {null,null}; Also end all lines with a semicolon ;


    Success
    Cor


    hi Cor. thanks for your tip.

    i found my mistake !!!. after clone dt1 to dt2 i wrote this code:

    dt2 = dt1.DefaultView.ToTable();

    after this code, my primary key was destroyed.

    what should i do?

    my whole code:

    dt2 = dt1.Clone();
    
    dt2 = dt1.DefaultView.ToTable();

    after above code dt2 does not contains primary key of dt1. why?

    Monday, March 12, 2012 5:09 AM
  • No it is not destroyed, 

    A datatable is a little bit strange class, it has two main collections in it which are depending to each other (this is in fact wrong in OO).

    If you do ToTable than a new DataTable is created. However, the columns property in the new table is than still pointing to the old Table.

    Therefore I recreated the keys new in the sample I've made for Steve yesterday (in C#) yesterday, it was by coincidence the same subject.

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/5d8a6731-33d2-40aa-9374-12098190bc03


    Success
    Cor


    Monday, March 12, 2012 8:06 AM
  • Why do you bother cloning to dt2 when you're creating a new instance of dt2 after that with a .ToTable()?


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Monday, March 12, 2012 3:17 PM
  • Why do you bother cloning to dt2 when you're creating a new instance of dt2 after that with a .ToTable()?


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

     hi.

    i clone dt1 to dt2 because of getting dt1 table schema and use .ToTable() because of getting filtered rows and filling dt2 with this rows.

    Saturday, March 17, 2012 7:01 AM
  • Couldn't you just use a DataView instead of dt2? Of course, I have no idea what you're doing with the two DataTables, so maybe you have to do it this way. Maybe an idea of the purpose for dt2 might help us make a better suggestion ... although, since there is a way to get your PrimaryKey back (the other thread that Cor linked to), maybe it doesn't matter anymore. I was just curious to know what your purpose is...

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Saturday, March 17, 2012 3:59 PM
  • Couldn't you just use a DataView instead of dt2? Of course, I have no idea what you're doing with the two DataTables, so maybe you have to do it this way. Maybe an idea of the purpose for dt2 might help us make a better suggestion ... although, since there is a way to get your PrimaryKey back (the other thread that Cor linked to), maybe it doesn't matter anymore. I was just curious to know what your purpose is...

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com


     it's easy and my purpose is simple too. I have some filtered row in dataview and want to copy them in to datatable but when i assign

    dt2 = dt1.DefaultView.ToTable();

     my primary key in dt1 table don't copy to dt2. i don't want to use "for" or "for each" code. is there any way or not?

     i clone dt1 to dt2 for copy primary key but above line of code destroyed primary key. (explained later )

    thanks.

    Sunday, March 18, 2012 5:26 AM
  • You already said this, that you want to copy the filtered data from dt1.DefaultView to a DataTable. My question is what are you doing with that second DataTable? Why wouldn't a DataView work for you?

    DataView dv = new DataView(dt1);
    dv.RowFilter = dt1.DefaultView.RowFilter;

    Use dv, instead of dt2. Of course any changes to data that are made to dv will affect dt1, so it's important to know what you need to do with that second view of your data ...


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Sunday, March 18, 2012 2:24 PM