[2008] Please help with bindingSource.AddNew
I appreciate you taking a look at my question!
So, I have a table that is bound to a view and I have a bindingsource on it. Usual stuff. I have a row, that the user wants to clone or make a copy of. So, I take the currently selected row and type cast it to a row and then make a call to AddNew. Then I copy all the data from the first to the new. All is good so far. The problem is that the primary key of the new row is -1 for the type casted new row. I have tried several things after I set all the data in the new row as you will see below to re-load this PK so I can pass it on to a new form to mod the data. One thing, I don't like this code but it was the only thing I could do to make a good copy of the original row. At one point, I tried doing the copy in 1 line of code. I copied all the data and the PK and thus caused an exception since the PK was no longer unique.
Any help is appreciated! Dave
Code:
Dim rowToCopy As cmaDBDataSet.cmaPropertyRecRow
rowToCopy = CType(CType(Me.CmaPropertyRecBindingSource.Current, DataRowView).Row, cmaDBDataSet.cmaPropertyRecRow)
Dim tmp, newID As Integer
tmp = rowToCopy.id ' id of row to be copied
' add a new row
Me.CmaPropertyRecBindingSource.AddNew()
' get the currently added row
Dim newrow As cmaDBDataSet.cmaPropertyRecRow
newrow = CType(CType(Me.CmaPropertyRecBindingSource.Current, DataRowView).Row, cmaDBDataSet.cmaPropertyRecRow)
newID = newrow.id ' id of new row
newrow.YearBuilt = rowToCopy.YearBuilt
newrow.ZipCode = rowToCopy.ZipCode
newrow.City = rowToCopy.City
newrow.propertyRef = 0
' Add it to the database
Me.Validate()
Me.CmaPropertyRecBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.CmaDBDataSet)
Me.CmaPropertyRecBindingSource.ResetCurrentItem()
newrow = CType(CType(Me.CmaPropertyRecBindingSource.Current, DataRowView).Row, cmaDBDataSet.cmaPropertyRecRow)
newID = newrow.id ' id of new row
'Me.CmaPropertyRecDataGridView.Refresh() this updates the view shown in this open / clone form
frmCMA.theIndex = newID
frmModCMA.theIndex = newID
frmModCMA.Show()
Re: [2008] Please help with bindingSource.AddNew
Is there a better way to make a copy of a row being loaded from a dataset?
Anyone?
Re: [2008] Please help with bindingSource.AddNew
Hmmm. Maybe this should probably be moved to the Database section ?