Question Clone data for new revision

dsk96m

Well-known member
Joined
Jan 11, 2013
Messages
173
Programming Experience
1-3
I have a form that loads to a selected row selected by the user. The controls are disabled so they cant make changes. What I would like to do is have a button they can click to create a new revision. The parent table has a column named revision for this purpose. So I would like the user to be able to click on the button to create a new revision, copy the data to a new record and increase the rev by one. There are also child tables associated with this form, so the data in those would have to be cloned as well.

What is the best way to go about this?

I have a typed dataset by the way.
 
So i figured out how to make it work for the parent table, but it blanks out all the values for the child tables. Not sure if this is the best way to do it. Here is the code to atleast get the parent row copied.
VB.NET:
        Dim drvold As DataRowView
        Dim drvnew As DataRowView

        'Copy Flight Card Data
        drvold = DirectCast(Me.FlightcardBindingSource.Current, DataRowView)
        drvnew = DirectCast(Me.FlightcardBindingSource.AddNew(), DataRowView)
        For Each dc As DataColumn In drvnew.Row.Table.Columns
            If Not dc.AutoIncrement Then
                drvnew.Row(dc) = drvold.Row(dc)
            End If
        Next
        Me.FlightcardBindingSource.ResetBindings(False)

        Me.RevisionSpinEdit.EditValue = Me.RevisionSpinEdit.EditValue + 1
 
If I meant the row ID I would have said the row ID. I said the row because I meant the row. If you have a DataTable bound to a BindingSource then the Current property of the BindingSource will return the DataRowView for the record currently selected in the UI. That DataRowView object has a Row property that returns the corresponding DataRow. That DataRow has a GetChildRows that will get the child DataRows for that parent DataRow over a specific DataRelation.
 
Ok, I think i got what you were saying.

VB.NET:
        Dim childrows() As DataRow
        Dim i As Integer

        Dim parentrow As DataRow = DirectCast(Me.FlightcardBindingSource.Current, DataRowView).Row
        childrows = parentrow.GetChildRows("fk_system_setup_flightcard")

so that puts the child rows into childrows(). All my tables have PKs with autoincrement id. So how do I insert those rows. Do i have to go through each column and skip the identity column?
Below is what I had figured out before your post

VB.NET:
        Dim drvold As DataRowView
        Dim drvnew As DataRowView
        drvold = DirectCast(Me.FlightcardBindingSource.Current, DataRowView)
        drvnew = DirectCast(Me.FlightcardBindingSource.AddNew(), DataRowView)
        For Each dc As DataColumn In drvnew.Row.Table.Columns
            If Not dc.AutoIncrement Then
                drvnew.Row(dc) = drvold.Row(dc)
            End If
        Next
        Me.FlightcardBindingSource.ResetBindings(False)

This works bc for the most part there is a 1 parent to 1 child relation. But i do not think it will work for my one child table that is 1 to many. I like your idea better and was able to get the parent and child row, now I just don't know how to take that child row and insert it as a new row without the identity column. Any thoughts?
 
You should be able to do something like this:
Dim destinationRow = table.NewRow()
Dim fieldValues = sourceRow.ItemArray

'Clear the PK column value.
fieldValues(0) = Nothing

destinationRow.ItemArray = fieldValues
table.Rows.Add(destinationRow)
 
So I got the new temp row created, it shows the correct values in the right places, but when I save it doesnt save the child tables. If I reselect the items, then save it works.
 
Back
Top