指示具有特定 ID 的组合框值?

Indicate combobox values with a specific ids?

我制作了一个 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

预期输出:

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

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

如果您提出,您可以使用 SelectedIndex 属性 或 ComboBox。此外,您需要将结果集存储在某些 class 字段中(最好是 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

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

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

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

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

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