vb.net - ComboBox with a BindingSource and a different DataSource

27,604

SelectedValue is used in conjunction with the ValueMember property, but since your array list doesn't have descriptive fields, that won't work.

Try using SelectedItem for your binding:

Me.ComboBox.DataBindings.Add(New Binding("SelectedItem", _
                                      Me.TblBindingSource, "ColumnName", True))
Share:
27,604
joharei
Author by

joharei

Computer science student and hobby programmer.

Updated on January 17, 2020

Comments

  • joharei
    joharei over 4 years

    In my form I have a DataGridView bound to a BindingSource that has a DataSet as DataSource. I also have some TextFields and ComboBoxes on the form that are bound to different columns in the DataSet through the BindingSource. The idea is that the values in the columns on the selected row in the DataGridView are reflected in the other controls on the form. Maybe I make it sound a bit complicated, but it's fairly easy to connect the TextFields, and also the ComboBoxes bound to tables in the dataset.

    My problem is that this time I want to set the items in the ComboBox from an array and not from a table in the DataSet. This is what I've tried:

    Me.ComboBox.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue",       Me.TblBindingSource, "ColumnName", True))
    
    Dim ItemArray(2) As String
    ItemArray(0) = ""
    ItemArray(1) = "Default"
    ItemArray(2) = "User-set"
    ComboBox.DataSource = ItemArray
    

    Now, this seems to work partially as the ComboBox is populated correctly, and I can select a value, and it appears in the DataGridView. But it doesn't update its selected value as I change rows in the DataGridView. The column ("ColumnName") is a ComboBoxColumn that gets its item list in the way shown above, and it seams to work as expected.

    If it wasn't clear; I have several ComboBoxes with similar functionality that works, but they are bound to a column in a DataTable, as follows:

    Me.ComboBox1.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.Tbl1BindingSource, "WiDMethodX", True))
    Me.ComboBox1.DataSource = Me.Tbl2BindingSource
    Me.ComboBox1.DisplayMember = "SomeColumn"
    Me.ComboBox1.ValueMember = "SomeColumn"
    

    If it matters, the DataSet comes from an Access Database.

  • joharei
    joharei almost 12 years
    Thanks, that seems to work! But now something strange is happening; when I select a value in one row and then change rows one at a time, the value for that column is overwritten with the newly selected value. It doesn't happen if I change to a non-adjacent row!
  • joharei
    joharei almost 12 years
    Acually, I don't have to change the value in a combobox column; if I move up and down rows, the values in the combobox columns are changed to those of the last row.