【问题标题】:Indicate combobox values with a specific ids?指示具有特定 ID 的组合框值?
【发布时间】:2020-01-21 07:20:22
【问题描述】:

我制作了一个 Sub,它将用贷款描述填充我的组合框。我想在selectedindexchanged 上获得loandescriptionloancode,而无需再次查询数据库。这可能吗?或者我应该查询数据库以获取指示的loancode

Private Sub LoanProducts()
    Using cmd As New SqlClient.SqlCommand("SELECT loancode,loandescription FROM LoanProducts", gSQlConn)
        Dim dt As New DataTable
        Dim da As New SqlClient.SqlDataAdapter
        dt.Clear()
        da.SelectCommand = cmd
        da.Fill(dt)
        For Each row As DataRow In dt.Rows
            CmbLoanAvail.Items.Add(row("loandescription"))
        Next row
    End Using
End Sub

预期输出:

每次组合框索引发生变化时,所选贷款描述的贷款代码都会显示在一个文本框中。

【问题讨论】:

  • 使用DataTable 作为组合框的数据源。将DisplayMember 属性设置为loandescription 列,将ValueMember 属性设置为loancode 列。选择项目后,获取SelectedValue 并将其转换为loancode 的类型。
  • @Jimi 你能在下面发表你的答案吗?
  • 如果只在组合框中填写描述而不在数据库中查询,则无法获取值。
  • 您没有指定平台。 ComboBox 可以是 WinForms、WPF、UWP、Office/VBA 控件(更多...)
  • @jimi windows 窗体

标签: vb.net winforms combobox


【解决方案1】:

DataTable 设置为组合框.DataSource 属性并使用相应的列名填充.ValueMember 和.DisplayMember 属性。

Private Sub LoanProducts()
    Dim query As String = "SELECT loancode, loandescription FROM LoanProducts"
    Using command As New SqlCommand(query, gSQlConn)
        Dim data As New DataTable
        Dim adapter As New SqlClient.SqlDataAdapter With
        {
            .SelectCommand = cmd
        }

        adapter.Fill(data)

        CmbLoanAvail.ValueMember = "loancode"
        CmbLoanAvail.DisplayMember = "loandescription"
        CmbLoanAvail.DataSource = data
    End Using
End Sub

用户做出选择时,你可以使用SelectionChangeCommitted事件来执行一些动作

Private Sub comboBox1_SelectionChangeCommitted(
    ByVal sender As Object, ByVal e As EventArgs) Handles comboBox1.SelectionChangeCommitted

    Dim combobox As ComboBox = DirectCast(sender, ComboBox)

    Dim selectedCode As String = combobox.SelectedValue.ToString()  // returns 'loancode'
End Sub

【讨论】:

  • 所选值以字符串形式提供输出,如果贷款代码为 LC001,该怎么办。再次将值放入教科书中,您需要将值转换为字符串。
【解决方案2】:

如果您可以将数据库行作为强类型类,您可以使用ItemSourceDisplayMemberValueMember,这将解决您的问题。

如果您提出,您可以使用ComboBoxSelectedIndex 属性。此外,您需要将结果集存储在某个类字段中(最好是Pirvate 之一)。请参阅下面的示例代码:

Public Class Form4
    Private _result As DataTable

    Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim dt As New DataTable
        dt.Columns.Add("col1")
        dt.Columns.Add("col2")
        dt.Rows.Add("val11", "val12")
        dt.Rows.Add("val21", "val22")
        ' at this point, we got our result from DB
        _result = dt

        For Each row As DataRow In dt.Rows
            ComboBox1.Items.Add(row("col1"))
        Next
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim selectedIndex = ComboBox1.SelectedIndex
        Dim selectedRow = _result(selectedIndex)
    End Sub
End Class

【讨论】:

  • 我认为循环在这里不是一个好主意,因为还有另一种填充组合框的方法。
  • @SurajKumar 没有定义任何类,很难使用ItemsSource,因此需要循环
【解决方案3】:

将您的数据库对象保留在使用它们的方法的本地,这样您就可以确保它们被关闭和处置。请注意第一行 Using 末尾的逗号。这包括同一 Using 块中的命令。

在 Using 块之后设置 DataSourceDisplayMemberValueMember,这样用户界面代码在连接被释放之前不会运行。

要获取贷款代码,您只需访问SelectValueComboBox

Private Sub LoanProducts()
    Dim dt As New DataTable
    Using gSqlConn As New SqlConnection("Your connection string"),
            cmd As New SqlClient.SqlCommand("SELECT loancode,loandescription FROM LoanProducts", gSqlConn)
        gSqlConn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    ComboBox1.DataSource = dt
    ComboBox1.DisplayMember = "loandescription"
    ComboBox1.ValueMember = "loancode"
End Sub

Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
    MessageBox.Show($"The Loan Code is {ComboBox1.SelectedValue}")
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 2022-11-20
    相关资源
    最近更新 更多