Click here to Skip to main content
13,201,050 members (52,484 online)
Rate this:
Please Sign up or sign in to vote.
See more:
A combobox is populated as follows:
DataTable dt = StatusGet();
            DataRow newRow = dt.NewRow();
            dt.Columns["FullName"].MaxLength = 50;
            newRow["StatusID"] = 0;
            newRow["FullName"] = "<-- Select Status -->";
            dt.Rows.InsertAt(newRow, 0);
            cboTaxStatus.DisplayMember = "FullName";
            cboTaxStatus.ValueMember = "StatusID";
            cboTaxStatus.DataSource = dt;
            cboTaxStatus.SelectedIndex = 0; //None selected...

Then I select an item from the combobox.
BUT, the selectedvalue does not get set and I get:

cboTaxStatus.SelectedValue --> shows as {}

Note that I expect to see the value of the StatusID i.e. 1 or 2 depending on which item is selecte dfrom the combobox.
Also, note that if I do this in production, I do get a value for the selectedValue but I get {} if this is run in development server.
The source where the data is retrieved to populate the combobox is exactly the same in both development and production.

Any thoughts please?
Posted 6-Feb-13 6:26am
Updated 6-Feb-13 7:28am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This approach is not good.

Here is a comprehensive approach: don't store string as the element of the list part of combo box. An object of any other type can be an element. The only problem is: what text will be shown in the item when it is presented in UI? The answer is: whatever is returned by the method ToString. So, the solution is: override the virtual method Object.ToString in your structure or class to be used as the list element.

Please see my past answers where it is explained with code sample. Note that the method you can use with ComboBox can also be used for ListBox and some other types, so don't be confused: all my answers are applicable to your case. Please see:
Windows Form ListBox Selections[^],
Send value from Combobox to numericUpDown in application from this site[^],
i want select item's quantity from combo-box and calculate price in list box automatically[^],
get text from ComboBox[^],
Displaying an image from a list box[^],
C# Dynamic GUI Object Generation[^],
C# I want to store a set values entered in say textbox controls to be saved under an item which is in the combo box.[^].

CPallini 6-Oct-14 16:30pm
Thank you, Carlo.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Use SelectedItem rather than SelectedValue. The SelectedItem will be type of DataRowView. You will get all values from the selected row by reading with either column index or column name.

DataRowView dr = (DataRowView)comboBox1.SelectedItem;
dr["StatusID"]  -->  This will give you the SelectedValue
arkiboys 7-Feb-13 2:30am
Still get {}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171020.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100