When is Winforms Combobox.SelectedValue a string?

E

Ethan Strauss

Hi,
I generally work on web apps, but I am dealing with a Winform right now
and may be missing something really basic.
I have a combobox and I would like to know what value has been selected.
When I try to get the value as a string I sometimes get errors because
Combobox.SelectedValue is a DataRowView and when I try to get the value by
first casting as a DataRowView I sometimes get errors because I can't cast a
string to a DataRowView! I have not been able to track down when the value is
what and, while I could check for type, that seems really cumbersome for just
getting a simple value! Anyone know what I am missing?
More details: The combobox starts life without any items. It gets a
datatable as a datasource when the Tab of a Tabcontrol, on which it resides,
is selected. The databinding looks like this:

public static void BindeNumToList(System.Windows.Forms.ListControl
theControl, Type EnumType)
{
theControl.DataSource = eNumByDescriptionAsDataTable(EnumType);
theControl.DisplayMember = "Displays";
theControl.ValueMember = "Values";
}

where eNumByDescriptionAsDataTable is a method which generates a simple
datatable with a "Values" column and a "Displays" column from an eNum type.

The code which tries to find the value of the combobox looks like this:
DataRowView SelectedRow = (DataRowView)KitUsedBox.SelectedValue;
KitUsed TheKit = (KitUsed)int.Parse(SelectedRow[0].ToString());
// KitUsed TheKit =
(KitUsed)int.Parse(KitUsedBox.SelectedValue.ToString());

The uncommented part sometimesworks and sometimes throws errors. When I
switch and just have the part the is currently commented, I get the same
general behavior. Sometimes works, sometimes throws errors.

Thanks for your help!
Ethan


Ethan Strauss Ph.D.
Bioinformatics Scientist
Promega Corporation
2800 Woods Hollow Rd.
Madison, WI 53711
608-274-4330
800-356-9526
ethan.strauss atsign promega.com
 
A

.\\\\axxx

From memory, you'll get this sort of behaviour when you have no
datasource (i.e. it either hasn'e been set or is set to null) - so I
would check that your BindeNumToList is working properly,
 

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