本文介绍了儿童的BindingSource绑定儿童的EntitySet不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,SAMPLEDATA,有一个子表,测量。在我的WinForm,frmMain,一个SAMPLEDATA对象绑定到SampleDataBindingSource;该MeasurementsBindingSource有SampleDataBindingSource作为它的数据源和测量它的数据成员。一组文本框绑定到SampleDataBindingSource的;一个DataGridView势必MeasurementsBindingSource。

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.

有关frmMain,我也有一个presenter类,$ P $段Pmain,其中包含了属性类型SAMPLEDATA的,CurrentSample。该 SampleDataBindingSource.DataSource 必将对$ P $段Pmain的CurrentSample财产。

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.

当在测量特性的足够的已分配,它计算FiringFactor和,如果FiringFactor不是1时,它增加了一个测量项目向CurrentSample的测量的EntitySet:

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

当我在DataGridView输入值CrucibleMass,CrucibleSampleMass和CrucibleSampleFiredMass的UpdateFiringFactor方法不正确地运行,我最终得到另一个测量产品加入CurrentSample的测量EntitySet的。但是,在DataGridView不显示新行和MeasurementsBindingSource只有1记录(但 CurrentSample.Measurements.Count = 2 )。

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).

为什么在 CurrentSample.Measurements 的变化不会传播到 MeasurementsBindingSource ?我曾尝试 MeasurementsBindingSource.ResetBindings(假) MeasurementsDataGridView.Refresh SampleDataBindingSource.ResetBindings(假),但似乎没有任何更新 MeasurementsBindingSource 或它的datagridview。

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.

推荐答案

这是我搜到的EntitySet / BindingSource的问题的解决方案:

Here's the solution I found to the entityset/bindingsource problem:

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

下面的表格截图:

而code-背后:

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事​​件的Sample类和形式OnRefreshMeasurementsBinding方法。通常设置MeasurementsBindingSource上的EntitySet似乎有点缺憾测量的GetNewBindingList方法,但它的工作原理。

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.

请参阅项目#3约EntitySets和BindingSource的和GetNewBindingList在此论坛发帖:

See Item#3 about EntitySets and BindingSource and GetNewBindingList in this forum posting:

http://www.infragistics.com/community/forums/t/ 43526.aspx

这篇关于儿童的BindingSource绑定儿童的EntitySet不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:59