none
Combo box data binding both SelectedValue and Text ? RRS feed

  • Question

  • Hi,

    I have a combo box and i am trying to data bind two properties (SelectedValue and Text ) of a combo box . One of the data binding (SelecetdValue) works other does not. Any idea why and how to get it work?

     .vt_cbx_mat.DataBindings.Add("SelectedValue", EQ_BS, dtMatID, True, DataSourceUpdateMode.OnPropertyChanged)
     .vt_cbx_mat.DataBindings.Add("Text", EQ_BS, dtMaterial, True, DataSourceUpdateMode.OnPropertyChanged)
    Thanks

    Sunday, September 8, 2019 4:00 PM

Answers

  • ok i found the problem. Somehow the index change event for my 3rd combo box (cascading combo box) was not firing but selected value change event is working. So added a property and bound it to selectedvalue change event. Now it works. Thanks Alex.      
    • Marked as answer by Shan1986 Thursday, September 12, 2019 8:39 AM
    Wednesday, September 11, 2019 6:44 AM

All replies

  • Hi,

    fix your code:

    ComboBox1.DataSource = EQ_BS
    ComboBox1.ValueMember = "dtMatID"

    see my demo:

    Public Class Form1
        Dim dt As New DataTable
        Dim EQ_BS As New BindingSource
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.Items.Clear()
            dt.Columns.Add("column1")
            dt.Columns.Add("column2")
            dt.Rows.Add(New Object() {"1", "A"})
            dt.Rows.Add(New Object() {"2", "B"})
            dt.Rows.Add(New Object() {"3", "C"})
            dt.Rows.Add(New Object() {"4", "D"})
            EQ_BS.DataSource = dt
    
            ComboBox1.DataSource = EQ_BS
            ComboBox1.ValueMember = "column2"
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            EQ_BS.MoveNext()
        End Sub
    
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 9, 2019 2:37 AM
    Moderator
  • Hi,

    fix your code:

    ComboBox1.DataSource = EQ_BS
    ComboBox1.ValueMember = "dtMatID"

    see my demo:

    Public Class Form1
        Dim dt As New DataTable
        Dim EQ_BS As New BindingSource
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.Items.Clear()
            dt.Columns.Add("column1")
            dt.Columns.Add("column2")
            dt.Rows.Add(New Object() {"1", "A"})
            dt.Rows.Add(New Object() {"2", "B"})
            dt.Rows.Add(New Object() {"3", "C"})
            dt.Rows.Add(New Object() {"4", "D"})
            EQ_BS.DataSource = dt
    
            ComboBox1.DataSource = EQ_BS
            ComboBox1.ValueMember = "column2"
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            EQ_BS.MoveNext()
        End Sub
    
    
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Hallo Alex, May be i did not explain it properly. I am trying to save the selected value and Display value (Text) of the combobox to a datatable which is bound like below

    .vt_cbx_mat.DataBindings.Add("SelectedValue", EQ_BS, dtMatID, True, DataSourceUpdateMode.OnPropertyChanged)
    .vt_cbx_mat.DataBindings.Add("Text", EQ_BS, dtMaterial, True, DataSourceUpdateMode.OnPropertyChanged)
    on indexchanged event "SelectedValue" get stored in datatable column "dtMatID" but Text property of combobox does not get stored in column "dtMaterial" Any idea?

    Monday, September 9, 2019 7:10 AM
  • Hi,

    .vt_cbx_mat.DataBindings.Add("SelectedValue", EQ_BS, dtMatID, True, DataSourceUpdateMode.OnPropertyChanged)

    Your method can't bind the combobox's selectedvalue to the Datatable column, right way:

    ComboBox1.DataSource = EQ_BS
    ComboBox1.ValueMember = "dtMatID"

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 9, 2019 7:32 AM
    Moderator
  • Hi,

    .vt_cbx_mat.DataBindings.Add("SelectedValue", EQ_BS, dtMatID, True, DataSourceUpdateMode.OnPropertyChanged)

    Your method can't bind the combobox's selectedvalue to the Datatable column, right way:

    ComboBox1.DataSource = EQ_BS
    ComboBox1.ValueMember = "dtMatID"

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Hallo Alex, Sorry again. My Combobox has a different Datasource (display member, value member) for populating combobox.

    I want to save the selection to datatable (both value member and text) and when user navigates through datagrid view combobox should be set to the value in datatable.

    Monday, September 9, 2019 9:55 AM
  • Hi,

    Do you want to bind SelectedValue and Text in combobox to another Datatable? For example, below, the filled combobox value is bound to dt2?

    Public Class Form1
        Dim dt1 As New DataTable
        Dim dt2 As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt1.Columns.Add("Column1")
            dt1.Columns.Add("Column2")
            dt1.Rows.Add(New Object() {"1", "A"})
            dt1.Rows.Add(New Object() {"2", "B"})
            dt1.Rows.Add(New Object() {"3", "C"})
            dt1.Rows.Add(New Object() {"4", "D"})
            ComboBox1.DataSource = dt1
            ComboBox1.DisplayMember = "Column1"
            ComboBox1.ValueMember = "Column2"
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, September 10, 2019 2:47 AM
    Moderator
  • Hi,

    Do you want to bind SelectedValue and Text in combobox to another Datatable? For example, below, the filled combobox value is bound to dt2?

    Public Class Form1
        Dim dt1 As New DataTable
        Dim dt2 As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt1.Columns.Add("Column1")
            dt1.Columns.Add("Column2")
            dt1.Rows.Add(New Object() {"1", "A"})
            dt1.Rows.Add(New Object() {"2", "B"})
            dt1.Rows.Add(New Object() {"3", "C"})
            dt1.Rows.Add(New Object() {"4", "D"})
            ComboBox1.DataSource = dt1
            ComboBox1.DisplayMember = "Column1"
            ComboBox1.ValueMember = "Column2"
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Hallo Alex,

    I am not asking for populating a combobox. I have this code from Peter for my cascading comboboxes. I have 3 cascading combo boxes and i want to store the selection of 3 combo boxes and retrieve the selections back when reading XML. I want to store "SelectedValue" from all 3 combo boxes and display value from 1 combo box.

    code for my cascading comboboxes

    https://social.msdn.microsoft.com/Forums/en-US/b2aa567b-8b7a-47ff-939e-40c630402896/add-combo-box-selected-value-to-data-table?forum=vbgeneral

    I bind the combo box properties as below to one of my datatable which will be exported as xml at the end

    .vt_cbx_eqp.DataBindings.Add("SelectedValue", EQ_BS, dtEQP, True, DataSourceUpdateMode.OnPropertyChanged)
    .vt_cbx_Typ.DataBindings.Add("SelectedValue", EQ_BS, dtTYP, True, DataSourceUpdateMode.OnPropertyChanged)
    .vt_cbx_mat.DataBindings.Add("SelectedValue", EQ_BS, dtMatID, True, DataSourceUpdateMode.OnPropertyChanged)
    
    .vt_cbx_mat.DataBindings.Add("Text", EQ_BS, dtMaterial, True, DataSourceUpdateMode.OnPropertyChanged)

    above 3 lines with selectedvalue WORKS without any problem . I dont know why that 4th line does not work. Any idea? 

    Thanks 



    • Edited by Shan1986 Tuesday, September 10, 2019 7:10 AM
    Tuesday, September 10, 2019 7:09 AM
  • ok i found the problem. Somehow the index change event for my 3rd combo box (cascading combo box) was not firing but selected value change event is working. So added a property and bound it to selectedvalue change event. Now it works. Thanks Alex.      
    • Marked as answer by Shan1986 Thursday, September 12, 2019 8:39 AM
    Wednesday, September 11, 2019 6:44 AM
  • Hi,

    I am glad you have got your solution, we appreciated you shared us your solution and mark it as an answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 12, 2019 6:07 AM
    Moderator