Results 1 to 10 of 10

Thread: [RESOLVED] How to show data on Drop Down List type combo box from a database?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    15

    Resolved [RESOLVED] How to show data on Drop Down List type combo box from a database?

    I have the following requirement.

    • I have a combo box which i want to fill with data from a database.
    • Combo box type is - DropDownList. (This is required so the user can not edit data.)
    • Can save the data from combobox to table using combobox.text property. But while it is a DropDownList type, its impossible to show data extracted from the table even though the values are identical.
    • Some one proposed me to use the select index option. But i couldn't figure out how.



    I already tried with the following procedure. But its not showing data in DropDownList combo boxes. (I have my own class which i have used below)

    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


    I also tried with the following method. But index value is always -1.

    cmbDisProfile.SelectedIndex = cmbDisProfile.FindStringExact(trim(tempTb.Rows(0).Item("it_discount_profile")))

    What am i doing wrong? Is there any other better way?. Any advice will be highly appreciated.

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: How to show data on Drop Down List type combo box from a database?

    You're missing the ValueMember property....
    Code:
    cmbDisProfile.DisplayMember = "discount_profile"
    cmbDisProfile.ValueMember = "discount_profile"
    cmbDisProfile.DataSource = tempTb
    Now when you bind a datatable to the combobox, it will automatically select the right item (presuming that the values actually match).

    -tg

    edit - also, don't set the selected Index... just set the .Value ...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: How to show data on Drop Down List type combo box from a database?

    Quote Originally Posted by techgnome View Post
    also, don't set the selected Index... just set the .Value ...
    That should be SelectedValue.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    15

    Thumbs up Re: How to show data on Drop Down List type combo box from a database?

    Quote Originally Posted by techgnome View Post
    You're missing the ValueMember property....
    Code:
    cmbDisProfile.DisplayMember = "discount_profile"
    cmbDisProfile.ValueMember = "discount_profile"
    cmbDisProfile.DataSource = tempTb
    Now when you bind a datatable to the combobox, it will automatically select the right item (presuming that the values actually match).

    -tg

    edit - also, don't set the selected Index... just set the .Value ...

    -tg
    Thanks

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    15

    Re: How to show data on Drop Down List type combo box from a database?

    Thank you

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    15

    Re: How to show data on Drop Down List type combo box from a database?

    Thank you.

  7. #7
    Lively Member
    Join Date
    Jan 2020
    Posts
    120

    Re: How to show data on Drop Down List type combo box from a database?

    Code:
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim colors = New Dictionary(Of String, String)()
            colors("10") = "Red"
            colors("20") = "Blue"
            colors("30") = "Green"
            colors("40") = "Yellow"
            ComboBox1.DataSource = New BindingSource(colors, Nothing)
            ComboBox1.DisplayMember = "Value"
            ComboBox1.ValueMember = "Key"
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MessageBox.Show(Convert.ToString(ComboBox1.Text + " " + ComboBox1.SelectedValue))
        End Sub
    End Class

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: How to show data on Drop Down List type combo box from a database?

    Quote Originally Posted by Prahlad View Post
    Code:
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim colors = New Dictionary(Of String, String)()
            colors("10") = "Red"
            colors("20") = "Blue"
            colors("30") = "Green"
            colors("40") = "Yellow"
            ComboBox1.DataSource = New BindingSource(colors, Nothing)
            ComboBox1.DisplayMember = "Value"
            ComboBox1.ValueMember = "Key"
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MessageBox.Show(Convert.ToString(ComboBox1.Text + " " + ComboBox1.SelectedValue))
        End Sub
    End Class
    While it isn't supposed to make a difference, sometimes it does ... set the .DataSoure last after setting the .DisplayMember and .ValueMember ... just make it a habit. Setting the .DataSource first can sometimes, sometimes, lead to odd display issues.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    15

    Re: How to show data on Drop Down List type combo box from a database?

    Quote Originally Posted by techgnome View Post
    While it isn't supposed to make a difference, sometimes it does ... set the .DataSoure last after setting the .DisplayMember and .ValueMember ... just make it a habit. Setting the .DataSource first can sometimes, sometimes, lead to odd display issues.

    -tg
    Thank you

  10. #10

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    15

    Re: How to show data on Drop Down List type combo box from a database?

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width