Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.

Răspuns display member and value member in combo box

  • 6 martie 2012 03:24
     
      Are cod

    hi.

    i have two combo boxes. the first one is where i store the IDNumbers and the other one is where i store the Name.

    the code when I choose from cboNames  then the cboID automatically changes is working well. my problem is when i double click my listview the cboNames has no values but the cboID has.

    my code----

    Private Sub cboName_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboName.SelectedValueChanged
            selectedText = cboName.SelectedIndex
            cboID.SelectedIndex = selectedText
        End Sub
    
    
    
    ................
    
    Dim strSQL As String = "SELECT * FROM classification"
                Dim da As New MySqlDataAdapter(strSQL, conBooks)
                Dim ds As New DataSet
                da.Fill(ds, "classification")
                With cboName
                    .DisplayMember = "ClassDesc"
                    .ValueMember = "ClassID"
                    .DataSource = ds.Tables("classification")
                End With

    i really2x need HELP.. T.T

Toate mesajele

  • 6 martie 2012 09:28
     
     Răspuns Are cod

    Why do you hav 2 comboBoxes? Are both of them connected with the same object (id and name)? If, no need of two, one will do just fine.

    What about your table names? IS "ClassDesc" you full name of the column?

    Here is an example how to bind and retreive data from bind comboBox:

    'lets assume your combobox has name comboBox1!!!
    'and lets bind id and a name to it (to one combobox)!!
    
    Dim strSQL As String = "SELECT * FROM classification"
    Dim da As New MySqlDataAdapter(strSQL, conBooks)
    Dim ds As New DataSet()
    da.Fill(ds, "classification")
    comboBox1.DisplayMember = "Name_Column"
    'change this
    comboBox1.ValueMember = "ID_Column"
    'change this
    comboBox1.DataSource = New BindingSource(ds.Tables("classification"), Nothing)
    
    'retreiving in selectedIndexChanged event:
    Dim view As DataRowView = TryCast(comboBox1.SelectedItem, DataRowView)
    'if ID column is an integer:
    Dim id As Integer = Integer.Parse(view("ID_Column"))
    Dim name As String = view("Name_Column").ToString()
    'now you can use id (value) and name (item) from comboBox to show or set some other...!

    Hope it helps,

    bye


    Mitja