473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 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 7214
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found...
1
by: KlaasIMN | last post by:
Using a combobox I want to retrieve the index of a given value in a combobox. object sel = combo.SelectedValue; string selectedvalue = sel.ToString(); ComboBox.ObjectCollection items =...
1
by: nickyw | last post by:
Hi, Can anyone help, I want to get a reference to a combobox item based on the item's value rather than the text (basically what I am asking is, is there a function like 'FindStringExact' for...
12
by: Dica | last post by:
i can't seem to find a way to cast the value in my comboBox to integer. i've tried the following: short iProjectID = Convert.ToInt16(cboProjects.SelectedValue); i keep getting a "Sepcified...
30
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
7
by: Simon Verona | last post by:
I posted this in dotnet.languages.vb.controls but thought I'd post here as well.. I have a combobox that is bound to a dataview generated from a dataset. The dataset has a single table...
2
by: Mark Rae | last post by:
Hi, Just looking for some v2 guidance here... In WebForms, the DropDownList control has an Items collection which contains ListItem objects which, in turn, very conveniently contain a text...
3
by: Nofi | last post by:
Hello, I'm trying to fill my combobox per code, not with a DataSet because I want to have an empty row in it and this wasn't possible when working with a DataSet. The fill works fine, but when I...
2
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.