Get the value of the selected item of a combobox [C#]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
Given a combobox (cbClient dropdown) - the user can select one of the items listed in the combobox and when he does I need to know what item he selected. The combobox is populated by a DataSet (as shown in the code below), everytime I try to get the value of the selected item I get "System.Data.DataRowView" instead ???

Code:
System.Data.DataSet ds = new System.Data.DataSet();
ds = Database.Read("select * from [TaskTimer$]");

cbClient.SelectedIndex = -1;
cbClient.DataSource = ds.Tables[0];
cbClient.DisplayMember = "CLIENTS";
cbClient.ValueMember = "CLIENTS";
...
... The user can do stuff like select a Client from the ComboBox DropDown
...
string sSelectedClient = cbClient.SelectedItem.ToString();

So I am trying to get sSelectedClient to be = the selected client from the DrownDown Combobox.... but thats not what I am getting.
Do I need to CAST it somehow, into like a DATASET and then extract it as such? If so how would I go about doing that?
Any help/hints would be greatly appreciated
 
C#:
string sSelectedClient = ((DataRowView)cbClient.SelectedItem).Row("CLIENTS")
 
Last edited:
techmanbd said:
string sSelectedClient = cbClient.text

Although this is perfectly valid, it can cause more problems if, for example, the DropDownStyle is 'DropDown' and not 'DropDownList'

This will allow the end user to type in "anything" they want in the combobox, and the above code will simply return what they typed, not what was selected.

Of course, if the DropDownStyle *is* DropDownList, then the above code will have the same effect as the code I posted.

B.
 
Back
Top