L
Lars Netzel
A little background:
I use three Datagrids that are in a child parent relation.
I Use Negative Autoincrement on the the DataTables and that's workning nice.
My problem is when I Update these grid and write to the database and I set
the new Primary Keys and related Fields to the new asigned atuonumbers in
the Access.
The following Sub is the RowUpdated for the TopGrid's Adapter that will get
the new ID and then set that as a ParentID in Datatable for the the Next
Grid.
'---------------------------------------------------------------------------
----------------------------------------------------------------------------
------------------------------------------------------------------------
Private Sub adpProjParts_RowUpdated(ByVal sender As Object, ByVal e As
System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles adpProjParts.RowUpdated
If e.StatementType = StatementType.Insert Then
Dim cmd As New OleDb.OleDbCommand("SELECT @@IDENTITY", e.Command.Connection)
Dim OldId As Long = e.Row()("PRP_ID") 'This is primary key
Dim NewID As Long = cmd.ExecuteScalar 'This is the New PrimaryKey ID
CopyDataViews()
e.Row()("PRP_ID") = NewID
e.Row().AcceptChanges()
Me.dvProjCodes_COPY.RowFilter = Nothing
Me.dvProjCodes_COPY.RowFilter = "prco_prpid=" & OldId
Dim I As Long
Dim alRows As New ArrayList
Dim drvRowIter As DataRowView
For I = Me.dvProjCodes_COPY.Count - 1 To 0 Step -1
alRows.Add(Me.dvProjCodes_COPY.Item(I))
Next
Me.dvProjCodes_COPY.RowFilter = Nothing
m_bIsSaving = True
For Each drvRowIter In alRows
drvRowIter.Row("prco_prpid") = NewID
Next
m_bIsSaving = False
End If
End Sub
'---------------------------------------------------------------------------
--------------------------
If I debug this Sub, everythign is fine and I everything seems to get the
right IDs when stepping thru it. BUT when I later check the database, One or
more of the Items in the next grid (that I just changed the Parent ID on
with this Sub) has gotten the old Negative Autonumber back.. and Of course
the relation in Access is lost.
I have been trying to figure out what rows are "changed" back to the
Negative ID and the only thing I have seen that's even remotly close, is
that it's often is the Row in the datagrid that I have my cursor on when I
trigger the Save (Update on the dataset). But that' snot completely true, it
also seems like the rows I have HAD selected and then Left also the Get
Negative ID back..
BUT.. here's the weird part.. it shouldn't matter what Rows I have selected
in the grids.. since the RowUpdated happens AFTER all that.. so I'm
completely Confused right now.
To me it seems like even though the RowUpdated Sub is working alright when
stepping thru it, some of the Items in the DataTable that I set the New
ParentID on, changes back after that...
I know it's hard to explain this problem...
PLEASE HELP!!!
/Lars Netzel
I use three Datagrids that are in a child parent relation.
I Use Negative Autoincrement on the the DataTables and that's workning nice.
My problem is when I Update these grid and write to the database and I set
the new Primary Keys and related Fields to the new asigned atuonumbers in
the Access.
The following Sub is the RowUpdated for the TopGrid's Adapter that will get
the new ID and then set that as a ParentID in Datatable for the the Next
Grid.
'---------------------------------------------------------------------------
----------------------------------------------------------------------------
------------------------------------------------------------------------
Private Sub adpProjParts_RowUpdated(ByVal sender As Object, ByVal e As
System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles adpProjParts.RowUpdated
If e.StatementType = StatementType.Insert Then
Dim cmd As New OleDb.OleDbCommand("SELECT @@IDENTITY", e.Command.Connection)
Dim OldId As Long = e.Row()("PRP_ID") 'This is primary key
Dim NewID As Long = cmd.ExecuteScalar 'This is the New PrimaryKey ID
CopyDataViews()
e.Row()("PRP_ID") = NewID
e.Row().AcceptChanges()
Me.dvProjCodes_COPY.RowFilter = Nothing
Me.dvProjCodes_COPY.RowFilter = "prco_prpid=" & OldId
Dim I As Long
Dim alRows As New ArrayList
Dim drvRowIter As DataRowView
For I = Me.dvProjCodes_COPY.Count - 1 To 0 Step -1
alRows.Add(Me.dvProjCodes_COPY.Item(I))
Next
Me.dvProjCodes_COPY.RowFilter = Nothing
m_bIsSaving = True
For Each drvRowIter In alRows
drvRowIter.Row("prco_prpid") = NewID
Next
m_bIsSaving = False
End If
End Sub
'---------------------------------------------------------------------------
--------------------------
If I debug this Sub, everythign is fine and I everything seems to get the
right IDs when stepping thru it. BUT when I later check the database, One or
more of the Items in the next grid (that I just changed the Parent ID on
with this Sub) has gotten the old Negative Autonumber back.. and Of course
the relation in Access is lost.
I have been trying to figure out what rows are "changed" back to the
Negative ID and the only thing I have seen that's even remotly close, is
that it's often is the Row in the datagrid that I have my cursor on when I
trigger the Save (Update on the dataset). But that' snot completely true, it
also seems like the rows I have HAD selected and then Left also the Get
Negative ID back..
BUT.. here's the weird part.. it shouldn't matter what Rows I have selected
in the grids.. since the RowUpdated happens AFTER all that.. so I'm
completely Confused right now.
To me it seems like even though the RowUpdated Sub is working alright when
stepping thru it, some of the Items in the DataTable that I set the New
ParentID on, changes back after that...
I know it's hard to explain this problem...
PLEASE HELP!!!
/Lars Netzel