简体   繁体   中英

VB.NET - Selecting Record In DataSet from Combo Box

Good evening all,

I am working on a windows forms application in vb.net using a remote MySQL database. I have populated a combo box from the database with names. What I am trying to do, is use the combo box as a navigator. When the user selects a name from the combo box, it should navigate to the matching record.

I have successfully populated the combo box using the following code:

    Public Sub populateSelector(sender As Object, e As EventArgs)
        Dim dbConn As New MySqlConnection(My.Settings.mydbConnStr)
        dbConn.Open()
        Dim sqlCboPop As String = "SELECT fldLastName, fldID FROM tblData WHERE fldLocation = '" & CurrentUserRecordSet & "'"
        Dim daCboPop As New MySqlDataAdapter(sqlCboPop, dbConn)
        Dim dsCboPop As New DataSet
        daCboPop.Fill(dsCboPop, "tblData")
        With tsbCboSelect.ComboBox
            .DataSource = dsCboPop.Tables("tblData")
            .DisplayMember = "fldLastName"
            .ValueMember = "fldID"
        End With
    End Sub

This part ^^^ works fine.

However, I have been to the end of the interwebs and back trying to figure out how to select a record in the dataset based on the combobox. I was able to successfully execute this in Access vba in a previous project, but have not been able to get it working in Vb.Net. Any help would be greatly appreciated.

  • M

The most common way to do this would be to wire up an Event Handler for a SelectedIndexChanged event. Something like:

Protected Sub comboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlClient.SelectedIndexChanged
    Dim idx As Integer = CInt(ComboBox.SelectedValue)

    'Use idx as a Parameter value for a query for the other information you need from the database.
End Sub

Be sure to edit this to suit your needs.

I demonstrated how to use Using...End Using blocks to ensure that your database objects are disposed. I also demonstrated how to use parameters. I had to guess at the datatype so check you database for the correct value.

After what you called CurrentUserRecordSet is set to a location value. The button can be clicked to fill a DataTable and the ComboBox.

When the selection in the combo changes you can retrieve the appropriate row from the DataTable with the Select method.

Private Sub GetData(Location As String)
    Using dbConn As New MySqlConnection(My.Settings.mydbConnStr),
            cmd As New MySqlCommand("Select * From tblData Where fldLoaction = @Location", dbConn)
        cmd.Parameters.Add("@Location", MySqlDbType.VarChar).Value = Location
        dbConn.Open()
        Using reader = cmd.ExecuteReader
            dt.Load(reader)
        End Using
    End Using
End Sub

'This is a very odd name for a location
'This should be clicked after a value for CurrentUserRecordSet is set from somewhere.
Private CurrentUserRecordSet As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    GetData(CurrentUserRecordSet)
    With tsbCboSelect.ComboBox
        .DisplayMember = "fldLastName"
        .ValueMember = "fldID"
        .DataSource = dt
    End With
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim ID = CInt(ComboBox1.SelectedValue)
    Dim row = dt.Select("fldID = ID")(0)
End Sub

For future visitors, this documentation may help.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listcontrol.valuemember?view=net-5.0

Be sure to change the language (upper right corner).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM