ComboBox HELP Pleasse!

G

Guest

I am using the following code as an example. Once the user selects an
item from the combobox (e.g., "Martin"), how can I access the value of
that item (e.g., "1")?

Dim dtAdjuster as new DataTable
dtAdjuster.Columns.add("Names")
dtAdjuster.Columns.add("Keys")
dtAdjuster.loaddatarow(new object() {"Martin", "1"},true)
dtAdjuster.loaddatarow(new object() {"Cor","2"},true)

Combobox1.datasource = dt
Combobox1.displaymember = "Names"
Combobox1.Valuemember = "Keys"

In the click method, I check the value of

Combobox1.SelectedValue.ToString

It is returning "System.Data.DataRowView" as the value of the
item. What is wrong? Any idea?

Thank you in advance.
 
N

Nathan Sokalski

You are missing the key component of code that involves databinding. You
must call the DataBind() method after setting the DataSource property. In
your case, it looks like you need the following line:

Combobox1.DataBind()

I think this will fix your problem, if not, let me know. Good Luck!
 
C

Cor Ligthert [MVP]

Nathan,

The DataBind is typical a webform method. A webform does not have a combobox
luckily for that they have named that a little bit the same control
DropDownList.

I hope this helps,

Cor
 
G

Guest

D,

I copied and pasted your code into a VS 2003 Windows Form application. After
fixing 1 typo, your code worked for me. I changed this line:

Combobox1.datasource = dt

to this line:

ComboBox1.DataSource = dtAdjuster

Kerry Moorman
 
C

Cor Ligthert [MVP]

Kerry,

It was some sample code I made, I did not see this one change.
(I don't see the reason, this is the only change I use forever dt)

Good catch,

:)

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