Question Binding to SelectedValue of a Combo Box

woodm1

New member
Joined
Sep 3, 2012
Messages
3
Programming Experience
Beginner
I've got a form with several controls on it, the values for which come from an XML file. This file needs to be updated as the user modifies the values in the controls and then saves.

Currently I've got all the textboxes and checkboxes working by adding bindings, like so:

VB.NET:
 Dim bs As New BindingSource        bs.DataSource = theDS.Tables(0)
        questionBinding = New Binding("Text", bs, "Question", True, DataSourceUpdateMode.Never)
        txtQuestion.DataBindings.Add(questionBinding)

Then I call questionBinding.WriteValue() to save changes.

I'm struggling with the comboboxes though. What I need is to have a static list of items in each combobox, with the comboboxes bound via their SelectedValue property to the datasource.

This is what I've got at the moment -

VB.NET:
 Dim item1 As New ComboItem
        item1.text = "Free Text"
        item1.SelectedValue = "TextBox"


        cmbAnsType.Items.Add(item1)     


        AnsTypeBinding = New Binding("SelectedValue", bs, "AnswerFormat", True, DataSourceUpdateMode.Never)
        AnsTypeBinding.ReadValue()


        cmbAnsType.DataBindings.Add(AnsTypeBinding)

But this isn't working. Changing the property to be bound in AnsTypeBinding to "text" displays what's in the datasource - but this isn't what needs to be displayed.

Any pointers would be much appreciated.
 
ComboBoxes are not bound via their SelectedValue property to their DataSource. You bind a list to the ComboBox via the DataSource property. You set the DisplayMember to the name of the property or column that you want displayed in the drop-down list. You set the ValueMember to the name of the property or column that you want exposed via the SelectedValue, which will usually be the primary key column. When the user selects an item in the list, the corresponding ID is then exposed via the SelectedValue property.
 
Thank you for your reply.

I guess it isn't possible to update my DataTable automatically when a user changes the selection in the ComboBox then, or to have the selected item in the combobox selected based on a single value in the Datatable. No worries; I'll get the value manually and set/save the selected item based on the DataTable cell value. Thanks for your help.
 
I think you'll find that what you want is possible, although you haven't described it very well. Let's say that you have two tables: Person and Status. The Status table contains StatusID and Description while the Person table contains PersonID, Name and StatusID, that last column being a foreign key. In your UI you want to display Person records using a TextBox for Name and a ComboBox for StatusID, with the ComboBox displaying the Description values from the Status table. That would look something like this:
Dim data As New DataSet

statusAdapter.Fill(data, "Status")
personAdapter.Fill(data, "Person")

'Bind the Status table to the ComboBox.
statusBindingSource.DataSource = data.Tables("Status")

With statusComboBox
    .DisplayMember = "Description"
    .ValueMember = "StatusID"
    .DataSource = statusBindingSource
End With

'Bind the Person table to the TextBox and the ComboBox.
personBindingSource.DataSource = data.Tables("Person")
nameTextBox.DataBindings.Add("Text", personBindingSource, "Name")
statusComboBox.DataBindings.Add("SelectedValue", personBindingSource, "StatusID")
 
Back
Top