Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
A combobox is populated as follows:
C#
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.Items.Clear();
            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?
Thanks
Posted
Updated 6-Feb-13 7:28am
v3

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.[^].

—SA
 
Share this answer
 
Comments
CPallini 6-Oct-14 16:30pm    
5.
Sergey Alexandrovich Kryukov 6-Oct-14 16:43pm    
Thank you, Carlo.
—SA
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
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900