Hello
I have been looking around for some sample code that is eluding me.
I have a small
vb.net app that allows a user to select a row in a datagrid control that launches a detail form. When I then update or change the record on the detail form and save, how do I refresh the datagrid on the master form?
I was told I need to write an event handler but I am not sure how. Here is what I wrote so far.
The form with the datagrid is called CustomerList
The detail form is called CustomerDetail
Here is the relevant code on the CustomerList form
Code:
'in the class header
Dim WithEvents cdetail As New CustomerDetail
'sub written to open customerdetail
Private Sub OpenCustomer()
Try
Dim rowview As Data.DataRowView
Dim selectedrow As DS.CustomerRow
rowview = CType(CustomerBindingSource.Current, DataRowView)
selectedrow = CType(rowview.Row, DS.CustomerRow)
cdetail.CustomerLoad(selectedrow.CustomerID)
If Not IsNothing(cdetail) Then
If Not cdetail.IsDisposed Then
cdetail.BringToFront()
cdetail.Show()
Else
cdetail = New CustomerDetail
cdetail.Show()
End If
Else
cdetail = New CustomerDetail
cdetail.Show()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
'calls the event handler to refill the dataset
Private Sub cdetail_DataHasChanged() Handles cdetail.DataHasChanged
Try
Me.CustomerTableAdapter.Fill(Me.DS.Customer)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Here is the code on the CustomerDetail side
Code:
'class header
Event DataHasChanged()
'custom load routine
Friend Sub CustomerLoad(ByVal CustomerID As Long)
Try
Me.CustomerTableAdapter.FillByCustomerID(DS.Customer, CustomerID)
Catch ex As Exception
End Try
End Sub
Private Sub CustomerBindingSource_ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles CustomerBindingSource.ListChanged
RaiseEvent DataHasChanged()
End Sub
I hope that is not confusing. I can break the code up further if it helps
Ryan