无法在 DropDownStyle (DropDownList) 的 ComboBox 控件中显示数据

Can't display Data in ComboBox Control of DropDownStyle (DropDownList)

我有以下需求,

我有一个 ComboBox 控件(DropDownList 样式),用户必须 select 给定值,但不能编辑。然后我将它保存到数据库 Table,它工作正常。

(dataRow("it_discount_profile") = Trim(cmbDisProfile.Text))

但是当我尝试通过从数据库中检索数据来在同一个 ComboBox 中显示相同的数据时,它不会显示。

(cmbDisProfile.Text = Trim(tempTb.Rows(0).Item("it_discount_profile")))

当我将 ComboBox 更改为 "DropDown Style" 时,它起作用了。但随后用户可以对其进行编辑。

我是不是漏掉了什么东西,还是那样?任何建议将不胜感激。

我用程序在 运行 时间内填写了它。

Private Sub filldisProfiles()
    Dim sqlString As String = "SELECT discount_profile FROM tb_discount_profiles"
    Dim tempTb As DataTable
    Dim myTbClass As myClassTableActivities = New myClassTableActivities()
    tempTb = myTbClass.myFunctionFetchTbData(sqlString)

    cmbDisProfile.DataSource = tempTb
    cmbDisProfile.DisplayMember = "discount_profile"
End Sub

好的。实际上,我正在尝试将我的一个旧项目从 VB 迁移到 VB.Net。 VB.Net 对我来说有点陌生。我正在使用自建的 class 来减少其他地方的代码。我在下面附上 class。

我的实际要求是从 table 填充组合框。我喜欢在 运行 时间内完成。我不希望用户编辑它。如果他们想添加一个新值,他们有单独的地方(表格)。我认为我做错了。如果可能,请提供示例代码,因为我不熟悉建议的方法。

Public Function myFunctionFetchTbData(ByVal inputSqlString As String) As DataTable
    Try
        Dim SqlCmd As New SqlCommand(inputSqlString, conn)
        Dim dataAdapter As New SqlDataAdapter(SqlCmd)

        Dim fetchedDataSet As New DataSet
        fetchedDataSet.Clear()
        dataAdapter.Fill(fetchedDataSet)

        Dim fetchedDataTable As DataTable = fetchedDataSet.Tables(0)

        Return fetchedDataTable

    Catch ex As Exception
        MsgBox(Err.Description)
    End Try
End Function

' this sub will update a table
Public Sub MyMethodUpdateTable(ByVal sqlString As String, ByVal tbToUpdate As DataTable)

    Dim SqlCmd As New SqlCommand(sqlString, conn)
    Dim dataAdapter As New SqlDataAdapter(SqlCmd)
    Dim objCommandBuilder As New SqlClient.SqlCommandBuilder(dataAdapter)
    dataAdapter.Update(tbToUpdate)

End Sub

Public Function MyMethodfindRecord(ByVal strSearckKey As String, ByVal tableName As String, ByVal strColumnName As String) As Boolean
    Try
        Dim searchSql As String = "SELECT * FROM " & tableName & " WHERE " & strColumnName & "='" & strSearckKey & "'"

        'Dim searchString As String = txtCategoryCode.Text
        '        searchOwnerCmd.Parameters.Clear()
        '        searchOwnerCmd.Parameters.AddWithValue("a", "%" & search & "%")

        Dim tempTb As DataTable
        Dim myTbClass As myClassTableActivities = New myClassTableActivities()
        tempTb = myTbClass.myFunctionFetchTbData(searchSql)

        If tempTb.Rows.Count = 0 Then
            Return False
        Else
            Return True
        End If

    Catch ex As Exception
        MsgBox(Err.Description)
    End Try
End Function
Public Function myFunctionFetchSearchTB(ByVal inputSqlString As String) As DataTable
    Try
        Dim SqlCmd As New SqlCommand(inputSqlString, conn)
        Dim dataAdapter As New SqlDataAdapter(SqlCmd)

        Dim fetchedDataSet As New DataSet
        fetchedDataSet.Clear()
        dataAdapter.Fill(fetchedDataSet)

        Dim fetchedSearchTB As DataTable = fetchedDataSet.Tables(0)

        Return fetchedSearchTB

    Catch ex As Exception
        MsgBox(Err.Description)
    End Try
End Function

好的。如果我理解正确,您在从数据库 Table 将数据 [String] 检索到 DropDownStyle [DropDownList 的 ComboBox 时遇到问题].

How do you fill/populate your ComboBox with Data From Database Table ?

在此 link 中,Microsoft 文档声明:

Use the SelectedIndex property to programmatically determine the index of the item selected by the user from the DropDownList control. The index can then be used to retrieve the selected item from the Items collection of the control.

用更通俗的英语 您永远不能通过代码在 DropDownList 中设置 ComboBox.Text 值,您已经通过测试代码知道了这一点,但是您需要使用 DisplayMember 和 ValueMember 或 SelectedIndex。

ComboBox1.SelectedIndex = ComboBox1.FindStringExact(Trim(tempTb.Rows(0).Item("it_discount_profile")))

请考虑使用(键,值)字典集合从数据库Table填充您的组合框控件,这里是example

谢谢大家的所有建议。唯一可以做到的方法就是你所说的方式。为了大家的利益,我想到了工作代码和一些要点。

提议,

  • ComboBox1.SelectedIndex = comboBox1.FindStringExact(Trim(tempTb.Rows(0).Item("it_discount_profile")))"

将数据表绑定到组合框时不起作用。如果上述 "SelectedIndex" 方法有效,应在 运行 时间内将值添加到组合框。向combobox添加item的代码如下(myClassTableActivities是我自己定义的class,如上所示),

Private Sub filldisProfiles()
    Dim sqlString As String = "SELECT discount_profile FROM tb_discount_profiles"
    Dim tempTb As DataTable
    Dim myTbClass As myClassTableActivities = New myClassTableActivities()
    tempTb = myTbClass.myFunctionFetchTbData(sqlString)

    Dim i As Integer = 0
    For i = 0 To tempTb.Rows.Count - 1
        cmbDisProfile.Items.Add(Trim(tempTb.Rows(i).Item("discount_profile")))
    Next
End Sub

添加后我们可以使用以下代码将数据显示在组合框(DropDownList)上。

Private Sub txtItCode_TextChanged(sender As Object, e As EventArgs) Handles txtItCode.TextChanged
    Try
        Dim sqlString As String = "SELECT * FROM tb_items where it_code='" & Trim(txtItCode.Text) & "'"
        Dim tempTb As DataTable
        Dim myTbClass As myClassTableActivities = New myClassTableActivities()
        tempTb = myTbClass.myFunctionFetchTbData(sqlString)

        If Len(txtItCode.Text) < 4 Then
            cmdAdd.Enabled = False
            cmdDelete.Enabled = False
            cmdSave.Enabled = False
        Else
            If tempTb.Rows.Count > 0 Then


                With tempTb
                    txtItName.Text = Trim(tempTb.Rows(0).Item("it_name"))
                    cmbDisProfile.SelectedIndex = cmbDisProfile.FindStringExact(Trim(tempTb.Rows(0).Item("it_discount_profile")))
                    cmbProfitProfile.SelectedIndex = cmbProfitProfile.FindStringExact(Trim(tempTb.Rows(0).Item("it_profit_profile")))
                    cmbItCategory.SelectedIndex = cmbItCategory.FindStringExact(Trim(tempTb.Rows(0).Item("it_category")))
                    cmbFinanCategory.SelectedIndex = cmbFinanCategory.FindStringExact((tempTb.Rows(0).Item("it_finance_category")))
                End With
                cmdAdd.Enabled = False
                cmdDelete.Enabled = True
                cmdSave.Enabled = True
            Else
                cmdAdd.Enabled = True
                cmdDelete.Enabled = False
                cmdSave.Enabled = False
                txtItName.Text = ""
                Call fillItCategories()
                Call fillProProfile()
                Call filldisProfiles()
                Call fillFinCat()
            End If
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try