Results 1 to 5 of 5

Thread: Fill combobox with column from database

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    30

    Fill combobox with column from database

    Anyone know a good tutorial that shows how to populate a combobox with data from a table column in a database? I've been trying for hours and can't get it to work.


    Thanks.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,678

    Re: Fill combobox with column from database

    What you're wanting to do is to bind your DataColumn to your ComboBox. This can be done several different ways, but to get a better understanding of which way is best for you, how are you accessing your data? Did you add the DataBase to your project via the wizard, are you connecting to your DataBase manually, etc.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,454

    Re: Fill combobox with column from database

    In short:
    Code:
    With myComboBox
        .DisplayMember = "NameOfColumnToDisplay"
        .ValueMember = "NameOfColumnIdentifyingRecord"
        .DataSource = myDataTable
    End With
    That can be done in code or the equivalent in the designer.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,400

    Re: Fill combobox with column from database

    Here's an example that shows some databinding methods

    https://code.msdn.microsoft.com/DataBinding-91dfbcf4

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    30

    Re: Fill combobox with column from database

    I have an existing project that I'm wanting to use a database to store information for. These are the steps I did -

    I added a new datasource and created a new database. I go to Database Explorer and add a table with two fields and then use Show Table Data to populate a few lines. I go to Configure Data Source with Wizard and add the table to the dataset. Right-clicking on the table shows the two fields, I set the first one to Combobox and drag it over to the form. I run the program and it doesn't show up.

    Okay that didn't work, so I start over with a new project. Once again I create a new database, add the table, populate the table, and then use the wizard to add the tables to the dataset. I go to the toolbox and add a combobox to the form, go to it's properties and set the Datasource to the table and set DisplayMember to the desired column. I save everything, run it and MY HEART IS POUNDING OUT OF MY CHEST BECAUSE IT WORKED.

    Now I go to what I've been trying to do all along - to get it to work in an existing combobox that's inside of a user control. I go to it and set it's Datasource and DisplayMember properties to the exact same settings, run the program - and it's blank.

    Is there something about a combobox being inside of a user control that prevents it from being populated by a data field? It's actually inside of a groupbox that's inside of a user control, maybe I have to set the data properties for those too...

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