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

问题描述 投票:1回答:1

我有以下要求:

我有一个组合框控件(DropDownList样式),用户必须选择一个给定的值,但不能编辑。然后我把它保存到一个数据库表中,工作正常。

(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对我来说有点陌生。我使用一个自建的classto减少其他地方的代码。我附上下面的类。

我的实际需求是从一个表中填充组合框。我喜欢在运行时做。我不想让用户编辑它。如果他们想添加一个新的值,他们有单独的地方(表格)。我想我这样做是错误的。如果可能的话,请给一个示例代码,因为我不熟悉建议的方法。

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
vb.net combobox data-retrieval
1个回答
0
投票

好的。如果我的理解是正确的,你有一个问题在检索数据[]。字符串从一个数据库表到一个DropDownStyle的组合框[]。下拉列表].

如何用数据库表中的数据填充 ComboBox?

在这个 联系,微软文档规定,该。

使用 选定索引 属性为 程序上 测定指数 选定项目 由用户从 下拉列表 控制。该。 指数 然后可以用来 从项目集合中检索所选项目。 的控件。

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

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

请考虑使用(Key,Value)从数据库表中填充你的ComboBox控件。词典集这里有一个 例子


0
投票

谢谢你们的建议。只有按u说的方法才能做到。我想把工作代码和一些要点放出来,供大家参考。

提出来的。

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

当你把datatable绑定到combobox时,不工作。如果要让上面的 "SelectedIndex "方法起作用,就应该在运行时将值添加到组合框中。向组合框中添加项目的代码如下(myClassTableActivities是我自己定义的一个类,如上图所示)。

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

添加后我们可以使用下面的代码在combobox(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
© www.soinside.com 2019 - 2024. All rights reserved.