471,348 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

When is Winforms Combobox.SelectedValue a string?

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.ToStri ng());

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
Jun 27 '08 #1
1 7075
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,
On May 16, 5:08 am, Ethan Strauss
<EthanStra...@discussions.microsoft.comwrote:
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.ToStri ng());

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
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Bill C. | last post: by
1 post views Thread by nickyw | last post: by
30 posts views Thread by dbuchanan | last post: by
2 posts views Thread by Mark Rae | last post: by
3 posts views Thread by Nofi | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.