【问题标题】:DataBinding: ComboBox.Text not updating when SelectedValue changes?DataBinding:当 SelectedValue 更改时,ComboBox.Text 不更新?
【发布时间】:2010-03-16 18:24:03
【问题描述】:

我有一个ProbationComboBox,其中SelectedValue 绑定到RegistrationBindingSourceDisplayMember 绑定到ProbationBindingSource

当我调用RegistrationBindingSource.ResetCurrentItem() 时,SelectedValue 属性会使用来自RegistrationBindingSource.ProbationID() 的正确值进行刷新,但Text 属性不会更新。

以下是我在 ComboBox 上设置数据绑定的方法:

ProbationComboBox.DataBindings.Add(New System.Windows.Forms.Binding( _
   "SelectedValue", Me.RegistrationBindingSource, "ProbationID", True))

ProbationComboBox.DataSource = Me.ProbationBindingSource
ProbationComboBox.DisplayMember = "probation"
ProbationComboBox.ValueMember = "id"

在我找出绑定问题之前,我将其用作临时修复:

DataRow row = CType(ProbationBindingSource.Current, DataRowView).Row
ProbationComboBox.Text = CType(row, RootNamespace.DataSet.probationRow).probation

有什么想法吗?谢谢!

【问题讨论】:

    标签: vb.net data-binding combobox bindingsource selectedvalue


    【解决方案1】:

    调用 ResetBindings 怎么样?

    帮助文件说这样做“会导致绑定到 BindingSource 的控件重新读取列表中的所有项目并刷新它们的显示值。”

    【讨论】:

    • 我试过这个,没有运气。我什至尝试将 True 作为参数传递,以强制重新读取架构,但它没有帮助。
    【解决方案2】:

    问题不在于数据绑定到RegistrationBindingSource,而在于我的数据和程序。这是发生了什么:

    1. 我会在记录加载之前清除所有ComboBox 控件的Text 属性。

    2. ProbationBindingSource.Current 已通过数据绑定或手动设置 ProbationComboBox.SelectedValue 属性得到更新。

    3. 在大多数情况下,学生没有试用期,ProbationBindingSource.Current 的值更新为与以前相同的值。因此,ProbationBindingSource.Position 没有更改,Text 属性也没有更新。

    我的最终解决方案会在清除 Text 属性之前检查给定的 SelectedValue 是否会更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      相关资源
      最近更新 更多