Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 Forms
Hi,
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.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 6-Feb-13 6:26am
arkiboys2.6K
Edited 6-Feb-13 7:28am
v3
Rate this: bad
good
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.[^].
 
—SA
  Permalink  
Comments
CPallini at 6-Oct-14 16:30pm
   
5.
Sergey Alexandrovich Kryukov at 6-Oct-14 16:43pm
   
Thank you, Carlo.
—SA
Rate this: bad
good
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  
  Permalink  
Comments
arkiboys at 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
0 Sergey Alexandrovich Kryukov 531
1 OriginalGriff 329
2 DamithSL 190
3 Maciej Los 180
4 Mehdi Gholam 135
0 DamithSL 95
1 OriginalGriff 65
2 Gopu Raj 30
3 Mahsa Hassankashi 25
4 Raul Iloc 25


Advertise | Privacy | Mobile
Web04 | 2.8.141030.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2014
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