Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to Get Selected Value from ComboBox

1 view
Skip to first unread message

Kali

unread,
Feb 13, 2010, 2:41:45 PM2/13/10
to
I'm having a difficult time getting the value from the selected item in a
combobox. Windows forms app.

I bind the combobox cboEmployee like so...

OleDbConnection cn = new
OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
cn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("select employee_number,
employee_name from employees", cn);
DataSet ds = new DataSet();
da.TableMappings.Add("Table", "employees");
da.Fill(ds);
cboEmployees.DataSource = ds.Tables["employees"];
cboEmployees.ValueMember = "employee_number";
cboEmployees.DisplayMember = "employee_name";
cn.Close();

Then I try to get the value (employee_number) from the combobox after it's
been selected (in the click even of a button)...

MessageBox.Show(cboEmployee.SelectedValue.ToString());

However, all I get returned is the employee name, not the employee number.
Any ideas why?

Thanks a lot.

James

RayLopez99

unread,
Feb 15, 2010, 4:48:29 AM2/15/10
to
On Feb 13, 9:41 pm, "Kali" <noem...@address.com> wrote:

>
> However, all I get returned is the employee name, not the employee number.
> Any ideas why?
>

See p. 98 of Karli Watson's book. I just checked and it's in there.
Too complicated to explain here, just buy the book. Combo boxes have
a property that binds them to the .XSD schematic. I also assume you
know about how list boxes/combo boxes can display a different value
than what is bound, but that's another issue.

RL

Beginning C# 2005 Databases (Programmer to Programmer) by Karli Watson
(Paperback - Oct. 16, 2006)

PvdG42

unread,
Feb 15, 2010, 12:43:06 PM2/15/10
to

"Kali" <noe...@address.com> wrote in message
news:OpmpXSOr...@TK2MSFTNGP02.phx.gbl...

You can also do a Google (or, your favorite search engine) search for:

"Beginning C# 2005 Databases (Programmer to Programmer) by Karli Watson"

and discover places to download sample chapters, etc.

0 new messages