none
display member and value member in combo box

    Pertanyaan

  • 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

    06 Maret 2012 3:24

Jawaban

  • 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

    • Disarankan sebagai Jawaban oleh AGovil 06 Maret 2012 17:15
    • Ditandai sebagai Jawaban oleh Bob Wu-MTModerator 12 Maret 2012 6:38
    06 Maret 2012 9:28