tables into combobox

johmolan

Well-known member
Joined
Oct 11, 2008
Messages
129
Programming Experience
Beginner
I am trying to list all the tables in my db in a combo box, but I just can't seem to find the correct way to do it.
I have set the datasource to my dataset but maybe I have to fill it somehow.

This is what I have done so far:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
ComboBox1.DataSource = KalkyletabellerDataSet

End Sub

Can someone please help me?
 
Why would you re-fill the combobox every time the user selects an item within the combobox? You will only need to fill the combbox once in a different event.

Also you need to set several combbox properties in order to bind it to your table. DataSource to the table that is being bound, DisplayMember to the column that will be displayed to user and ValueMember to your hidden value if needed (ex key value to each item)


VB.NET:
    With ComboBox1
        .DataSource = Dataset.TableName
        .DisplayMember = "TABLES"
        .ValueMember = "Id"
    End With
 
As I tried to explain, I would like to list up all the names of the tables in the db in a combobox, not any value in them
 
As I tried to explain, the hidden value (Value Member) was optional. My instructions answer your question if you but look.

Attached is a working example. Just run the sample, add your server name & database name to connect too and it will retrieve the list of tables from that database and display the list in the combobox.
 

Attachments

  • ListDatabaseTables.zip
    15.2 KB · Views: 46
I am using an Access database, I proberbly should have spesified that in the first post.
since your example is for a sql or serverbased database
 
Ok the sample wont run without making some changes then for Access, but the prinicipal is still the same. Look specifically at how I bound the combobox. DataSource will equal your datatable and DisplayMember will equal the field within the table you want to display.
 
I am really trying to make this work, but I can't seem to make it work.

I only want the names of the tables into the combobox, this way I can choose which table I want to get more data from.into a new combobox
 
Why would you re-fill the combobox every time the user selects an item within the combobox? You will only need to fill the combbox once in a different event.
He isnt doing that, he's assigning the datasource, which is a different thing to filling. It might however, cause a problem in that the index will just back to 0 index each time..
 
I am really trying to make this work, but I can't seem to make it work.

Find out how to get a list of all the tables in an access database, using SQL

Read the DW2 link in my signature, section: Creating a Simple Data Application

Understand how to add a new tableadapter to a project (NOT a dataadapter)

Add a new tableadapter using the query that pulls the names of all table

In the datasources window, expand the node representing the query for all tables, and in the list underneath change the table name from TextBox to ComboBox
Drag it to the form

-

SOme of the things in this post will only make sense if you read the DW2 tutorial
 
The list of tables shouldnt be that many, if your still having trouble binding just concentrate on getting your query to return the table names first. You can always loop through the results and add them to your combobox at that point.
 
Back
Top