none
Cloning a Datarow for Reuse in a DataTable RRS feed

  • Question

  • I am working in vb2008. I need to be able to create an array of cloned datarows for reuse in the same datatable.   I am using a treeview where each node has a datarow linked to the treenode's tag property.  What I need to do is use the treeview to select a series of datarows that I would like to clone for use elsewhere in the datatable.  I have 1 datatable called dt.  My code follows:

    For each node as treenode in tree.selectednode.nodes
    if node.checked is true then
    Dim SourceDataRow as datarow=ctype(node.tag,datarow)
    next

    This code identifies all the datarows that I have checked in the tree for cloning.  I would like on each iteration of my loop to clone the SourceDataRow into an array of datarow objects that I can later add back as new nodes into my datatable (dt).


    Thanks,
    Fred

    Monday, June 8, 2009 1:15 AM

Answers

  • Hey Fred,

    Why do you need to have a "cloned" row?I understand that you are associating each row with a node. But why do they have to be cloned ones? can you not just assign a DataRow that is not cloned? Do you need the cloned rows for a newly created rows? Or in the UI is there a clone option to clone a DataRow to create another new one? Are you using these cloned rows for changetracking purposes?

    Until you answer some more questions I can only guess at a solution. Here is something thing that may help though.

    take your datatable and call .clone on it to create another Datatable. What clone does is it creates a new DataTable with the same schema. Then what you do is call copy and this will copy the changes over from one DataTable to another. Next then use your algorithm above to assign rows from the new datatable that was just created to the nodes. Then when the person hits save, call GetChanges on the cloned DataTable, this will get you a list of rows. Take these rows and use Merge to merge them back into the original DataTable. Then use the adapters to push the data to the database using adapter.Update.

    Thanks
    Chris Robinson
    Program Manager - DataSet
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, June 8, 2009 3:01 PM

All replies

  • Hey Fred,

    Why do you need to have a "cloned" row?I understand that you are associating each row with a node. But why do they have to be cloned ones? can you not just assign a DataRow that is not cloned? Do you need the cloned rows for a newly created rows? Or in the UI is there a clone option to clone a DataRow to create another new one? Are you using these cloned rows for changetracking purposes?

    Until you answer some more questions I can only guess at a solution. Here is something thing that may help though.

    take your datatable and call .clone on it to create another Datatable. What clone does is it creates a new DataTable with the same schema. Then what you do is call copy and this will copy the changes over from one DataTable to another. Next then use your algorithm above to assign rows from the new datatable that was just created to the nodes. Then when the person hits save, call GetChanges on the cloned DataTable, this will get you a list of rows. Take these rows and use Merge to merge them back into the original DataTable. Then use the adapters to push the data to the database using adapter.Update.

    Thanks
    Chris Robinson
    Program Manager - DataSet
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, June 8, 2009 3:01 PM
  • Thanks for some ideas.  What I am doing is assembling a message from a collection of data items which are represented by treenodes.  I have business logic which tells me how to assemble a message from the data nodes.  The problem occurs when the business logic I am using wants to use a single datanode multiple times in a message. 

    I have datanodes called  RecordingTypes, Discriminators, and FieldNodes.  Each of these datatypes is represented by its own tree.  I want to use the data from these trees to help me to assemble a new tree which represents the total message.  Say if I have 10 fieldnodes which I need to add to my message, I wanted to check those nodes in the fieldnode tree  and create some type of collection which I could then use to generate new treenodes/datarows in the message hierarchy.  Because I am dealing with different trees, I need to gather my source nodes/datarows into a collection and then select the location in my message tree where I want to create new treenodes/datarows which uses the data from the source trees. 

    Hope this helps,
    Fred
    Monday, June 8, 2009 6:17 PM
  • Hey Fred,

    So it appears you have a Data Model, (RecordingTypes, Discriminators, and FieldNodes) which all all pieces of a Message. The pieces are related I'm not exactly sure how but then map this to a tree control.

    I don't understand the DataSet question here. Is there particular functionality that you need to understand of DataSet?

    To me it seems you are adding a lot of code over your data layer, perhaps if you create relationships with FK's and such between your different data it would simplify things. Not sure I can offer much help here unless there is a more specific question.

    Thanks
    Chris Robinson
    Program Manager - DataSet


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, June 9, 2009 12:15 AM