I have a table, SampleData, that has a child table, Measurements. On my WinForm, frmMain, a single SampleData object is bound to the SampleDataBindingSource; the MeasurementsBindingSource has SampleDataBindingSource as its datasource and Measurements as its DataMember. A set of textboxes are bound to SampleDataBindingSource; a datagridview is bound to MeasurementsBindingSource.
For frmMain, I also have a presenter class, preMain, which contains a property, CurrentSample, of type SampleData. The SampleDataBindingSource.DataSource
is bound to the CurrentSample property of preMain.
When enough of the properties in Measurements have been assigned, it calculates the FiringFactor and, if the FiringFactor is not 1, it adds another Measurement item to the CurrentSample's Measurement entityset:
Partial Class Measurement
Private Sub UpdateFiringFactor()
Dim necessaryDataIsAvailable As Boolean = (Me.CrucibleMass IsNot Nothing And _
Me.CrucibleSampleFiredMass IsNot Nothing And _
Me.CrucibleSampleMass IsNot Nothing)
If necessaryDataIsAvailable Then
Me.FiringFactor = CDbl((Me.CrucibleSampleFiredMass - Me.CrucibleMass) / (Me.CrucibleSampleMass - Me.CrucibleMass))
If Me.FiringFactor <> 1 Then
Me.SampleData.AddNewMeasurement()
End If
End If
End Sub
Private Sub OnCrucibleMassChanged()
UpdateFiringFactor()
End Sub
Private Sub OnCrucibleSampleFiredMassChanged()
UpdateFiringFactor()
End Sub
Private Sub OnCrucibleSampleMassChanged()
UpdateFiringFactor()
End Sub
End Class
When I enter values for CrucibleMass, CrucibleSampleMass, and CrucibleSampleFiredMass in the datagridview, the UpdateFiringFactor method does run correctly and I eventually get another Measurement item added to CurrentSample's Measurements entityset. However, the datagridview does not show a new row and the MeasurementsBindingSource only has 1 record (but CurrentSample.Measurements.Count = 2
).
Why does the change in CurrentSample.Measurements
not propogate to MeasurementsBindingSource
? I have tried MeasurementsBindingSource.ResetBindings(False)
, MeasurementsDataGridView.Refresh
, SampleDataBindingSource.ResetBindings(False)
, but nothing seems to update MeasurementsBindingSource
or its datagridview.
Here's the solution I found to the entityset/bindingsource problem:
Here's a screenshot of the form:
And the code-behind:
The solution hinges on the RefreshMeasurementsBinding event in the Sample class and the OnRefreshMeasurementsBinding method in the form. Frequently setting the MeasurementsBindingSource to the GetNewBindingList method on the Measurements entityset seems a bit kludgy, but it works.
See Item#3 about EntitySets and BindingSource and GetNewBindingList in this forum posting:
http://www.infragistics.com/community/forums/t/43526.aspx