473,241 Members | 1,661 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,241 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 7204
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.