Rob
Rob

Reputation: 5286

DataBinding: ComboBox.Text not updating when SelectedValue changes?

I have a ProbationComboBox with the SelectedValue bound to a RegistrationBindingSource, and the DisplayMember bound to a ProbationBindingSource.

When I call RegistrationBindingSource.ResetCurrentItem(), the SelectedValue property is refreshed with the correct value from RegistrationBindingSource.ProbationID(), but the Text property isn't updated.

Here's how I setup data-binding on the ComboBox:

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

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

Until I can figure out the problem with my binding, I've using this as a temporary fix:

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

Any ideas? Thanks!

Upvotes: 1

Views: 4584

Answers (2)

Rob
Rob

Reputation: 5286

The problem was not with data binding to the RegistrationBindingSource, but with my data and procedures. Here's what was happening:

  1. I would clear the Text property of all ComboBox controls before a record load.

  2. ProbationBindingSource.Current got updated, either by data binding or by manually setting ProbationComboBox.SelectedValue property.

  3. In most cases, students were not on probation, and the value of ProbationBindingSource.Current got updated with the same value as before. As a result, ProbationBindingSource.Position didn't change and the Text property wasn't updated.

My final solution checks if a given SelectedValue is going to be updated before clearing the Text property.

Upvotes: 1

John
John

Reputation: 16007

What about calling ResetBindings?

The help files say that doing this "causes a control bound to the BindingSource to reread all the items in the list and refresh their displayed values."

Upvotes: 1

Related Questions