How to Get Selected Value from ComboBox

K

Kali

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
 
R

RayLopez99

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)
 
P

PvdG42

Kali said:
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

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.
 

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