Newbie - Combobox Fill and Update Confusion

R

Randy

Hi, I'm venturing out from VBA into VB Express 2005. I've searched
MSDN and this group for what I think should be a simple and common
exercise - populating a combobox with the values in a table and
allowing the user to enter a new value, which would then be added to
the table.

I've made a really simple example to try to accomplish this. I've
taken the Northwind db and added the Categories table to my data
sources. Then I dragged the CategoryID field onto the form as a
combobox as well as the CategoryName and Description fields as text
boxes. In the properties for the CategoryID combobox, I've attempted
to bind the data with "CategoriesBindingSource - CategoryID" in each
of the four DataBindings options (SelectedItem, SelectedValue, Tag,
Text) since I don't the various roles of these four options.

When I build the app and click the CategoryID combobox, there is
nothing there. My understanding is that I shouldn't need any code to
accomplish this, but maybe I'm wrong. Clearly, I'm wrong about
something! Can somebody please explain this process to me? I'm
spinning my wheels and not getting anywhere.

Much appreciated!
Randy
 
R

RobinS

You're "overbinding". For your combobox, bind the CategoryID to the
ValueMember, and bind the CategoryDescription to the DisplayMember.

Robin S.
 
R

Randy

So your saying the I'm all bound up? (sorry). I tried your suggestion
and made some progress. I bound the CategoryID to the ValueMember as
you suggest. However, I could not locate the DisplayMember in the
properties for the CategoryName or Description fields. Where do I
look for this?

When I ran the app, both the Name and Description fields populated
with the values for the first record. However, the combobox for the
CategoryID field is still empty. Strange, since this is the field
that I was able to bind. Can you see where I am going wrong?

Thanks very much for your help!
Randy
 
R

RobinS

The DisplayMember and ValueMember are both part of the DataBindings on the
Combobox.

Dim dataSource as CustomersDataSet = new CustomersDataSet()
Dim adapter as CustomersTableAdapter = new CustomersTableAdapter()
adapter.Fill(dataSource)
m_Combo.DataSource = dataSource.Customers
m_Combo.DisplayMember = "CompanyName"
m_Combo.ValueMember = "CustomerID"

You can also bind it using the designer. But check the bindings for the
combobox after you do it and make sure they are right.

Robin S.
 
R

Randy

Hi Robin,
Thanks for the reply. I've inserted your code but I'm getting the
following error messages on the dim statements:

Type 'CustomersDataSet' is not defined
Type 'CustomersTableAdapter' is not defined

I have statements "Imports System.Data" and "Imports
System.Data.SqlClient" in the module in case its relevant.

Any ideas?
Randy
 
R

RobinS

That was an example, not the exact code for you to use. Change it to match
your case.

Robin S.
 
C

Cor Ligthert [MVP]

Randy be aware that the code from Robin looks very much C#

In VB.Net it will be

Dim dataSource as New CustomersDataSet()
Dim adapter as CustomersTableAdapter New
SQLClient.SQLDataAdapter(SQLString,Connection)
adapter.Fill(dataSource)
m_Combo.DataSource = dataSource.Customers
m_Combo.DisplayMember = "CompanyName"
m_Combo.ValueMember = "CustomerID"

Cor
 
C

Cor Ligthert [MVP]

Cor Ligthert said:
Randy be aware that the code from Robin looks very much C#

In VB.Net it will be

Dim dataSource as New CustomersDataSet()
 
R

RobinS

Cor,

My posting was in VB, not C#. It was an example using a strongly typed
dataset against the Northwind database. In C#, it would have the types
before the variables in the definitions, with semi-colons on the end.

Give me a tiny little bit of credit, please. :)

Randy -- I think I answered your question in a different thread. If not,
post back here and I will answer it here.

Robin S.
 
C

Cor Ligthert [MVP]

Robin,

Don't worry, do it myself as well, and have only fun seeing somebody doing
the same.
new = New

MyType a = new MyType();
dim a as New Mytype

I find that VBNet way in this nicer.

Cor
 
R

RobinS

I'm not worried, but I *am* confused. Show me what part of this is C#:

Dim dataSource as CustomersDataSet = new CustomersDataSet()
Dim adapter as CustomersTableAdapter = new CustomersTableAdapter()
adapter.Fill(dataSource)
m_Combo.DataSource = dataSource.Customers
m_Combo.DisplayMember = "CompanyName"
m_Combo.ValueMember = "CustomerID"

Robin S.
p.s. I like VB better, too, but just got hired for a C# job, darn it.
 
C

Cor Ligthert [MVP]

I'm not worried, but I *am* confused. Show me what part of this is C#:
Dim dataSource as CustomersDataSet = new CustomersDataSet()
Dim adapter as CustomersTableAdapter = new CustomersTableAdapter() "
Dim dataSource As New CustomersDataSet
Dim adapter As New CustomersTableAdapter
"

However it does not hurt at all, but you can see that you are writting C# at
the moment
 
R

RobinS

Cor,

It isn't C# and won't work in C# because it's VB code. It's the same as
yours, but it's the long way to say it. Both yours and mine will work.

I like to use the long version because it feels more specific, and being
specific makes me happy. :)

As I'm sure you know, in C# it would be

CustomersDataSet dataSource = new CustomersDataSet();
CustomersTableAdapter adapter = new CustomersTableAdapter();


Robin S.
 

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