【问题标题】:VB.NET - Selecting Record In DataSet from Combo BoxVB.NET - 从组合框中选择数据集中的记录
【发布时间】:2021-08-07 04:39:53
【问题描述】:

大家晚上好,

我正在使用远程 MySQL 数据库在 vb.net 中开发 Windows 窗体应用程序。我已经从数据库中填充了一个带有名称的组合框。我想要做的是使用组合框作为导航器。当用户从组合框中选择一个名称时,它应该导航到匹配的记录。

我已使用以下代码成功填充组合框:

    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

这部分^^^工作正常。

但是,我已经到了互联网的尽头,并试图弄清楚如何根据组合框在数据集中选择一条记录。在以前的项目中,我能够在 Access vba 中成功执行此操作,但无法使其在 Vb.Net 中工作。任何帮助将不胜感激。

  • M

【问题讨论】:

  • 您应该在DataSource 之前设置DisplayMemberValueMember
  • 你为什么要声明参数 sender 和 e 而你从不在 sub 中使用它们?
  • 你为什么要打开连接。 DataAdapter 将打开并关闭连接,如果它关闭,但如果它是打开的,它会保持打开状态。
  • 什么是 CurrentUserRecordSet?不要连接字符串来构建 sql 语句。始终使用参数。
  • 为什么还要使用 DataAdapters 和 DatasSets?它们在有用之前就超出了范围。

标签: mysql vb.net winforms


【解决方案1】:

最常见的方法是为SelectedIndexChanged 事件连接一个事件处理程序。比如:

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

请务必根据您的需要进行编辑。

【讨论】:

  • 应该是CInt(ComboBox.SelectedValue)
【解决方案2】:

我演示了如何使用Using...End Using 块来确保您的数据库对象被释放。我还演示了如何使用参数。我不得不猜测数据类型,因此请检查数据库中的正确值。

在您将CurrentUserRecordSet 设置为位置值之后。可以单击该按钮来填充 DataTable 和 ComboBox。

当组合中的选择发生变化时,您可以使用Select 方法从DataTable 检索适当的行。

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

【讨论】:

    【解决方案3】:

    对于未来的访问者,此文档可能会有所帮助。

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

    请务必更改语言(右上角)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 2021-02-23
      • 1970-01-01
      相关资源
      最近更新 更多