我有一个表,的sampleData,有一个子表,测量。 在我的WinForm,frmMain,单一的sampleData对象绑定到SampleDataBindingSource; 该MeasurementsBindingSource有SampleDataBindingSource作为它的数据源和测量它的数据成员。 一组文本框被绑定到SampleDataBindingSource的; 一个DataGridView势必MeasurementsBindingSource。
对于frmMain,我也有一个主持人类,倍美力,其中包含类型的sampleData的属性,CurrentSample。 该SampleDataBindingSource.DataSource
势必倍美力的CurrentSample财产。
当测量的性能足以已被分配,它计算FiringFactor和,如果FiringFactor不为1,它增加了一个测量项目的CurrentSample的测量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
当我在datagridview的输入值CrucibleMass,CrucibleSampleMass和CrucibleSampleFiredMass的UpdateFiringFactor方法不正确地运行,我终于得到另一个测量项目添加到CurrentSample的测量EntitySet的。 然而,在DataGridView不显示新行和MeasurementsBindingSource只有1条记录(但CurrentSample.Measurements.Count = 2
)。
为什么在改变CurrentSample.Measurements
不会传播到MeasurementsBindingSource
? 我曾尝试MeasurementsBindingSource.ResetBindings(False)
, MeasurementsDataGridView.Refresh
, SampleDataBindingSource.ResetBindings(False)
,但似乎没有任何更新MeasurementsBindingSource
或其datagridview的。
这是我发现的EntitySet的/ BindingSource的问题的解决方案:
Imports System.ComponentModel
Partial Class Measurement
Public Sub ChangeCrucibleMass(ByVal thisMass As Double)
CrucibleMass = thisMass
UpdateFiringFactor()
End Sub
Public Sub ChangeCrucibleSampleMass(ByVal thisMass As Double)
CrucibleSampleMass = thisMass
UpdateFiringFactor()
End Sub
Public Sub ChangeCrucibleSampleFiredMass(ByVal thisMass As Double)
CrucibleSampleFiredMass = thisMass
UpdateFiringFactor()
End Sub
Private Sub UpdateFiringFactor()
If AllDataAreAvailable() Then
FiringFactor = (CrucibleSampleFiredMass - CrucibleMass) / (CrucibleSampleMass - CrucibleMass)
Me.Sample.OnMeasurementsChanged(Nothing, Nothing)
End If
End Sub
Private Function AllDataAreAvailable() As Boolean
AllDataAreAvailable = False
Dim allFieldsHaveValue As Boolean = (CrucibleMass IsNot Nothing And CrucibleSampleFiredMass IsNot Nothing And CrucibleSampleMass IsNot Nothing)
If allFieldsHaveValue Then
Dim denominatorIsNotZero As Boolean = (CrucibleSampleMass - CrucibleMass) <> 0
Return denominatorIsNotZero
End If
End Function
End Class
Partial Class Sample
Public Sub OnMeasurementsChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Handles Me.PropertyChanged
If Me.Measurements.Count > 0 AndAlso Me.Measurements.Last.FiringFactor IsNot Nothing AndAlso Me.Measurements.Last.FiringFactor <> 1 Then
Me.Measurements.Add(New Measurement With {.CrucibleMass = Me.Measurements.Last.CrucibleMass})
RaiseEvent RefreshMeasurementsBinding()
End If
End Sub
Private Sub OnCreated()
Me.Measurements.Add(New Measurement)
End Sub
Public Event RefreshMeasurementsBinding()
End Class
下面是形式的截图:
和代码隐藏:
Imports System.ComponentModel
Public Class Form1
Private WithEvents newSample As Sample
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
newSample = SampleConduit.GetSample(1)
'newSample = New Sample
Me.SampleBindingSource.DataSource = newSample
OnRefreshMeasurementsBinding()
End Sub
Private Sub OnRefreshMeasurementsBinding() Handles newSample.RefreshMeasurementsBinding
Me.MeasurementsBindingSource.DataSource = newSample.Measurements.GetNewBindingList
End Sub
Private Sub MeasurmentsDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles MeasurmentsDataGridView.CellClick
Me.MeasurementsBindingSource.EndEdit()
Dim currentMeasurement = CType(Me.MeasurementsBindingSource.Current, Measurement)
Select Case e.ColumnIndex
Case 0
currentMeasurement.ChangeCrucibleMass(GetMeasurement)
Case 1
currentMeasurement.ChangeCrucibleSampleMass(GetMeasurement)
Case 2
currentMeasurement.ChangeCrucibleSampleFiredMass(GetMeasurement)
End Select
End Sub
Private Function GetMeasurement() As Double
Return CDbl(InputBox("Measurement:", "Get Measurement", "0"))
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim currentSample = CType(Me.SampleBindingSource.Current, Sample)
SampleConduit.SaveSample(currentSample)
End Sub
End Class
该解决方案取决于在样本类的RefreshMeasurementsBinding事件和在表格中OnRefreshMeasurementsBinding方法。 通常设置MeasurementsBindingSource对EntitySet的似乎有点缺憾的测量方法GetNewBindingList,但它的作品。
见项目#3关于此论坛发帖EntitySets和的BindingSource和GetNewBindingList:
http://www.infragistics.com/community/forums/t/43526.aspx