无法在DropDownStyle(DropDownList)的ComboBox控件中显示数据 [英] Can't display Data in ComboBox Control of DropDownStyle (DropDownList)

查看:126
本文介绍了无法在DropDownStyle(DropDownList)的ComboBox控件中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下要求,

我有一个ComboBox控件(DropDownList样式),用户必须选择一个给定值,但不能编辑。然后,我将其保存到数据库表中,并且工作正常。

I have a ComboBox Control (DropDownList Style) which user has to select a given value, but can not edit. Then I save it to a Database Table, and it's working fine.

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

但是当我尝试通过从相同的组合框中显示相同的数据时数据库,它不会显示。

But when I try to show the same Data in the same ComboBox by retrieving it from the Database, it won't show.

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

当我将ComboBox更改为 DropDown Style时,它可以工作。但是然后用户可以对其进行编辑。

When I change the ComboBox to "DropDown Style", it works. But then the User can edit it.

我在这里缺少什么吗?任何建议将不胜感激。

Am I missing something here or is it like that? Any advice will be highly appreciated.

我在运行时使用一个过程来填充它。

Im filling it in runtime using a procedure.

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对我来说并不新鲜。我使用自建的类来减少其他地方的代码。我在下面附上这个类。

Ok. Actually, Im trying to migrate one of my old project from VB to VB.Net. VB.Net is little new to me. Im using a self built classto reduce codes in other places. Im attaching the class below.

我的实际要求是从表格中填充组合框。我喜欢在运行时这样做。我不希望用户对其进行编辑。如果他们想添加一个新值,则为此有一个单独的位置(窗体)。我认为我做错事了。如果可能的话,请提供示例代码,因为我对建议的方法不熟悉。

My actual requirement is to populate the combo box from a table. I like to do it in run time. I don't want users to edit it. If they want to add a new value, they have separate place (Form) for that. I think im doing it in a wrong way. If possible please give a sample code since I'm not familiar with the proposed method.

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


推荐答案

确定。如果我理解正确,则在从数据库表中将数据[ String ]检索到DropDownStyle [ DropDownList ]的组合框中时遇到问题。

OK. If I understood correctly, you have a problem in retrieving Data [String] from a Database Table into a ComboBox of DropDownStyle [DropDownList].


如何用数据库表中的数据填充/填充组合框?

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

在此链接,Microsoft文档指出,

In this link, Microsoft docs state, that:


使用 SelectedIndex >属性,以编程方式确定用户从 DropDownList 控件中选择的的索引
。然后,可以使用
索引从控件的项目
集合中检索所选项目。

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。

In much more plain English You can never SET ComboBox.Text Value while in DropDownList by code, which you already knew by testing your code, but you need to use DisplayMember and ValueMember or SelectedIndex.

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

请考虑使用(Key,Value)从数据库表填充ComboBox控件词典集合,这是一个示例

Please consider populating your ComboBox Control from Database Table using (Key,Value) Dictionary collection, here is an example

这篇关于无法在DropDownStyle(DropDownList)的ComboBox控件中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆