M
Mika M
Hello,
My Windows Forms -application Opens DataBase connection when form is
loading, and creates DataSet for parent- and two same level child tables (1
to many relation in both) like following code shows
Private ds As DataSet
Private daParent, daChild1, daChild2 As OleDbDataAdapter
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
OpenDatabase()
CreateDataset()
....
End Sub
Private Sub CreateDataSet()
Dim strSQL As String
Try
ds = New DataSet("MyDataSetName")
'// *** Set up DataAdapter for Master table ***
strSQL = "SELECT ID, [Field1,...N] FROM MyParentTable"
daParent = New OleDbDataAdapter(New OleDbCommand(strSQL, cn))
'// *** Set up DataAdapter for 1.st Child table ***
strSQL = "SELECT ID, ParentID, [Field1,...N] FROM MyChildTable1"
daChild1 = New OleDbDataAdapter(New OleDbCommand(strSQL, cn))
'// *** Set up DataAdapter for 2.nd Child table ***
strSQL = "SELECT ID, ParentID, [Field1,...N] FROM MyChildTable2"
daChild2 = New OleDbDataAdapter(strSQL, cn)
'// *** Get the data into DataSet ***
daParent.Fill(ds, "Parent")
daChild1.Fill(ds, "Child1")
daChild2.Fill(ds, "Child2")
'// *** Create relations ***
ds.Relations.Add("ParentChild1", ds.Tables!Parent.Columns!ID,
ds.Tables!Child1.Columns!ParentID)
ds.Relations.Add("ParentChild2", ds.Tables!Parent.Columns!ID,
ds.Tables!Child2.Columns!ParentID)
'// *** In this case I'm using C1FlexGrid, but should work like DataGrid in
this code ***
flexParent.SetDataBinding(ds, "Parent")
flexChild1.SetDataBinding(ds, "Parent.ParentChild1")
flexChild2.SetDataBinding(ds, "Parent.ParentChild2")
Catch ex As Exception
MessageBox.Show(ex.Message, "Error (CreateDataset)", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub
Navigation works fine, but ...
My problem is that DataSet is updating after editing any of FlexGrid's
because ds.HasChanges() is still False after editing. I'm trying to make my
application to save changes immediately after editing any of those three
FlexGrids, but first I was trying the following code...
Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuSave.Click
Try
If ds.HasChanges() Then
Dim cb As New OleDbCommandBuilder(daParent)
daParent.UpdateCommand = cb.GetUpdateCommand()
daParent.Update(ds, "Parent")
ds.AcceptChanges()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error (mnuSave.Click)", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub
Whats wrong - or did I understood whole thing wrong way - with my code
because ds.HasChanges() is still False after
editing and Update is not yet committed ? Is there samples available for
this kind of problem, I mean how to save changes when Parent (Master) or
Child (Detail) Grid was edited ?
My Windows Forms -application Opens DataBase connection when form is
loading, and creates DataSet for parent- and two same level child tables (1
to many relation in both) like following code shows
Private ds As DataSet
Private daParent, daChild1, daChild2 As OleDbDataAdapter
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
OpenDatabase()
CreateDataset()
....
End Sub
Private Sub CreateDataSet()
Dim strSQL As String
Try
ds = New DataSet("MyDataSetName")
'// *** Set up DataAdapter for Master table ***
strSQL = "SELECT ID, [Field1,...N] FROM MyParentTable"
daParent = New OleDbDataAdapter(New OleDbCommand(strSQL, cn))
'// *** Set up DataAdapter for 1.st Child table ***
strSQL = "SELECT ID, ParentID, [Field1,...N] FROM MyChildTable1"
daChild1 = New OleDbDataAdapter(New OleDbCommand(strSQL, cn))
'// *** Set up DataAdapter for 2.nd Child table ***
strSQL = "SELECT ID, ParentID, [Field1,...N] FROM MyChildTable2"
daChild2 = New OleDbDataAdapter(strSQL, cn)
'// *** Get the data into DataSet ***
daParent.Fill(ds, "Parent")
daChild1.Fill(ds, "Child1")
daChild2.Fill(ds, "Child2")
'// *** Create relations ***
ds.Relations.Add("ParentChild1", ds.Tables!Parent.Columns!ID,
ds.Tables!Child1.Columns!ParentID)
ds.Relations.Add("ParentChild2", ds.Tables!Parent.Columns!ID,
ds.Tables!Child2.Columns!ParentID)
'// *** In this case I'm using C1FlexGrid, but should work like DataGrid in
this code ***
flexParent.SetDataBinding(ds, "Parent")
flexChild1.SetDataBinding(ds, "Parent.ParentChild1")
flexChild2.SetDataBinding(ds, "Parent.ParentChild2")
Catch ex As Exception
MessageBox.Show(ex.Message, "Error (CreateDataset)", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub
Navigation works fine, but ...
My problem is that DataSet is updating after editing any of FlexGrid's
because ds.HasChanges() is still False after editing. I'm trying to make my
application to save changes immediately after editing any of those three
FlexGrids, but first I was trying the following code...
Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuSave.Click
Try
If ds.HasChanges() Then
Dim cb As New OleDbCommandBuilder(daParent)
daParent.UpdateCommand = cb.GetUpdateCommand()
daParent.Update(ds, "Parent")
ds.AcceptChanges()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error (mnuSave.Click)", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub
Whats wrong - or did I understood whole thing wrong way - with my code
because ds.HasChanges() is still False after
editing and Update is not yet committed ? Is there samples available for
this kind of problem, I mean how to save changes when Parent (Master) or
Child (Detail) Grid was edited ?