ComboBox Operation RRS feed

  • Question

  • Hi:

    I have a combobox bound to a datatable with two columns. One column is "SE_KEY" the other column is "SE_NAME".

            cboSE.DataSource = bsSE
            cboSE.DisplayMember = "SE_NAME"
            cboSE.ValueMember = "SE_KEY"

    In the SelectedValueChanged Event I want to capture the value of "SE_KEY".

      cboSE.DataBindings.Add("SelectedValue", bsUS, "US_SE_KEY")

    The "SE_KEY" value will be placed into "US_SE_KEY" in the main datatable.

    I have spent a lot of time researching this but everything has failed so far.

    Could someone please assist me? Thanks.

    Saturday, May 9, 2020 8:08 PM

All replies

  • Hi

    Here is some simple code that may help. A stand alone example.I am wondering if the emboldened item in the code below is what you want.

    ' blank Form1 
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim dt As New DataTable("Freddy")
      Dim BS As New BindingSource
      Dim WithEvents cb As New ComboBox
      Dim lab1, lab2 As New Label
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With dt
          .Columns.Add("SE_KEY", GetType(String))
          .Columns.Add("SE_NAME", GetType(String))
          'some dummy data
          .Rows.Add("AAA", "AAAAAAA")
          .Rows.Add("BBB", "BBBBBBB")
          .Rows.Add("CCC", "CCCCCCC")
          .Rows.Add("DDD", "DDDDDDD")
        End With
        BS.DataSource = dt
        With cb
          .Location = New Point(20, 40)
          .DataSource = BS
          .DisplayMember = "SE_NAME"
          .ValueMember = "SE_KEY"
          .AutoCompleteMode = AutoCompleteMode.SuggestAppend
          .DropDownStyle = ComboBoxStyle.DropDown
          .AutoCompleteSource = AutoCompleteSource.ListItems
        End With
        With lab1
          .Location = New Point(20, 70)
          .AutoSize = True
          .Text = Nothing
        End With
        With lab2
          .Location = New Point(220, 70)
          .AutoSize = True
          .Text = Nothing
        End With
        Controls.AddRange({cb, lab1, lab2})
      End Sub
      Private Sub cb_SelectedValueChanged(sender As Object, e As EventArgs) Handles cb.SelectedValueChanged
        Dim cb As ComboBox = DirectCast(sender, ComboBox)
        If cb.Items.Count < 1 Then Exit Sub
        lab1.Text = "Selected:  " & cb.Text
        lab2.Text = "KEY:  " & cb.SelectedValue.ToString
      End Sub
    End Class

    Regards Les, Livingston, Scotland

    Saturday, May 9, 2020 8:54 PM
  • Hi Developer Dude,

    How is the question going? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao

    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, May 25, 2020 7:16 AM