Comboboxes and queries

N

Nicolae Fieraru

Hi All,

I have a database with two tables. One is called tblMainCategory and the
other one tblCategory. The tblMainCategories has fields Index1 and
MainCategory.
The tblCategory has Index2, MainCategoryId and Category.

I have a form with two comboboxes. I want to populate the first combobox
with the strings from MainCategory field. Then, when I select one of the
values from the dropdown, I want to populate the second combobox only with
those records.

I decided to try first using datareader. I can populate the first combobox.
But then I realised I need the value of the Index1 from tblMainCategories,
in order to be able to query the elements for the second combobox. I suppose
I could use another query, an ExecuteScalar to retrieve the corresponding
Index1 for MainCategory which was chosen from the dropdown.

Is there a simpler way than this one? For example I can see that a combobox
has a ValueMember property and a DisplayMember property. Is that a list or
what? If it is a list, then I can add the Index1 from the query. I tried
with ComboBox->ValueMember->Items->Add, but it doesn't work...


Regards,
Nicolae
 
C

Cor Ligthert

Nicolae,

As soon as you need two items than the datareader becomes not the best way
to fill a combobox.
You can do it be creating a class and from that objects to put in the items,
however very insufficient (when you need more than 2 items this will be the
way to go).

The datatable is the way to go with 2 items. It is this in code in vb style
(which last is only important for the first 2 rows) xxx is for OleDB or for
SQLClient or for OracleClient

\\\
dim dt as new datatable
dim da as xxxDataAdapter as new xxxDataAdapter(SqlSelectString,conn)
da.fill(dt)
combobox1.DataSource = dt
combobox1.DisplayMember = "mydisplaymember"
combobox1.ValueMember = "myValuemember"
///

That is all,

I hope this helps

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top