adding rows from one datatable to another

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sam

    adding rows from one datatable to another

    Hi,
    The following code :


    For Each r As DataRow In dtFlow.Rows
    Dim arow As DataRow = dtAllFlows.NewR ow
    arow = r
    dtAllFlows.Rows .Add(arow)
    Next

    leads to the error 'This row already belongs to another table '
    at the line dtAllFlows.Rows .Add(arow).
    I guess I should just do dtAllFlows = dtFlow.copy, but I can't as the
    code above is contained in a For loop and at each iteration, I add new
    rows (dtFlow changes at each iteration).

    How can I solve this problem ?

    Regards

  • Cor Ligthert

    #2
    Re: adding rows from one datatable to another

    Sam,

    I assume that this is what you want to do

    importrow


    I hope this helps,

    Cor


    Comment

    • Sam

      #3
      Re: adding rows from one datatable to another

      Thanks Cor ;-)

      Comment

      • Sam

        #4
        Re: adding rows from one datatable to another

        Actually, I might have spoken too quickly.

        For Each r As DataRow In dtFlow.Rows
        Dim arow As DataRow = dtAllFlows.NewR ow
        arow = r
        dtAllFlows.Impo rtRow(arow)
        Next

        It looked right but in fact when I try to access the data:

        dtallflows.Rows (0).Item(0)

        gives :

        Run-time exception thrown : System.IndexOut OfRangeExceptio n - Cannot
        find column 0.

        What's going on ?

        Comment

        • Cor Ligthert

          #5
          Re: adding rows from one datatable to another

          Sam

          When you try to copy a complete table, than you can use table.copy

          However this is a sample from me how to sort a datatable, you see than the
          use of the import in it.

          \\\
          Dim dv As New DataView(dt)
          dv.Sort = "bla"
          Dim dtnew As DataTable = dt.Clone
          For Each dvr As DataRowView In dv
          dtnew.ImportRow (dvr.Row)
          Next
          ///

          I hope this helps,

          Cor


          Comment

          • Sam

            #6
            Re: adding rows from one datatable to another

            As I said in the first thread, I don't want to do a copy as I add the
            rows in a for loop (concatenate if you prefer). therefore I have to use
            ImportRow, which leads to the error mentionned in my second thread...
            Can you help further ?

            Thx

            Comment

            • Cor Ligthert

              #7
              Re: adding rows from one datatable to another

              Sam,.

              The only thing you can do is copy it, use an extra dataview, remove it from
              the first.

              However you cannot have a reference in one datarow to two tables.

              There is this property in the datarow that should explain that to you if my
              text did not.



              It is a litte bit difficult to set one address to two tables.

              I hope this helps,

              Cor


              Comment

              • Sam

                #8
                Re: adding rows from one datatable to another

                Ok, I do that now :

                Dim dvTmp As new DataView
                dvTmp.Table = dtFlow
                dtFlow = Nothing
                For Each r As DataRowView In dvTmp
                dtAllFlows.Rows .Add(DirectCast (r, DataRow))
                Next

                But the Add line doesn't work ( invalid cast ). How can I add a
                datarowview to a datatable ??

                Thx

                Comment

                • Sam

                  #9
                  Re: adding rows from one datatable to another

                  This should be correct I guess...

                  dtAllFlows.Rows .Add(r.Row)

                  But I keep getting the error ; the datarow already exists in another
                  table :(
                  How come ??

                  Comment

                  • Cor Ligthert

                    #10
                    Re: adding rows from one datatable to another

                    Sam,

                    Did you read my message?

                    How you want to do this

                    myproperty a = adress of datatable wherein it is connected
                    datarow.a = table1
                    datarow.a = both table1 and table2

                    Cor


                    Comment

                    • Sam

                      #11
                      Re: adding rows from one datatable to another

                      Cor,
                      I read your message but :

                      1. the link u gave me does not work
                      2. I don't understand this sentence :

                      The only thing you can do is copy it, use an extra dataview, remove it
                      from
                      the first.

                      Could you explain that to me if you don't mind please ?

                      Thx

                      Comment

                      • Cor Ligthert

                        #12
                        Re: adding rows from one datatable to another

                        Sam,

                        Again.

                        You cannot add a reference from a datarow to 2 datatables. (That is what you
                        do with Add or insertAt)

                        Therefore is the only thing that you can do.
                        Create a copy from the original and add that to your new table
                        Remove the reference from the first and than add it to the new table
                        Create an extra dataview instead of that extra table.

                        The copy you have already, the second seems for me out of sense and the
                        thirth is probably what you need.

                        dim dv as new dataview(mydata table)

                        In fact you have now a complete view on your table. If you want only some
                        rows than you need a rowfilter

                        dv.rowfilter = "City = 'Amsterdam'"

                        I hope this helps

                        Cor


                        Comment

                        • Sam

                          #13
                          Re: adding rows from one datatable to another

                          I'm very very confused. Sorry :(
                          Are you actually saying I should do that :

                          1. Create a copy from the original and add that to your new table
                          dim dtAllFlows as datatable
                          dtAllFlows = dtFlow.copy

                          2.Remove the reference from the first and than add it to the new table
                          ???? what does this mean ?

                          3. Create an extra dataview instead of that extra table.
                          What for ? :
                          Dim dvTmp As new DataView
                          dvTmp.Table = dtFlow

                          Could you give a very simple example please, as I really don't
                          understand the above ?
                          Sorry for bothering :(
                          Thx

                          Comment

                          • Cor Ligthert

                            #14
                            Re: adding rows from one datatable to another

                            Sam,

                            I am only saying that you cannot have the same datarow in 2 datatables.

                            However you only tell that you want this and I only try to explain you why
                            this is impossible.

                            Cor


                            Comment

                            • Sam

                              #15
                              Re: adding rows from one datatable to another

                              I understood this. But you've also tried to explain a work around and I
                              dont understand it !

                              You said :
                              Therefore is the only thing that you can do.
                              Create a copy from the original and add that to your new table
                              Remove the reference from the first and than add it to the new table
                              Create an extra dataview instead of that extra table.


                              Could you give a little example of this (just a few lines of code) as I
                              can't get it.

                              Thx a lot

                              Comment

                              Working...