Problem with ComboBox.SelectedValue
-
Hi all, ComboBox.SelectedValue dont seem to work correctly. Code:
...
void SetupCustomers(){ //called from form constructor
cbCust.DataSource = dbase.GetCustomers(); //returns Customer[] from database
cbCust.DisplayMember = "Name"; // The name property
cbCust.ValueMember = "ID"; // The ID property (PK in database)
}
...However when calling
cbCust.SelectedValue
returns the wrong value. Calling((Customer)cbCust.SelectedItem).ID
gives the correct answer. This seems to be a problem with the DisplayMember being (automatically) sorted as all the returned SelectedValue's are within range. EG : Customers[] expanded may look like this: ID(Value) Name(Display) 20 Ben 21 John 23 Ann 26 Frank 27 Arnold The combobox will display the Displaymembers in alphebetical order. Here is where the problem lies (i think :)): Value Display 20 Ann 21 Arnold 23 Ben 26 Frank 27 John Now this is how Mr. ComboBox display and intepret Value and Display members, thus getting the mappings all wrong. If anyone has had a similar experience or can spot where I made a mistake, please tell me :) Thanking all replies in advance Cheers :) READ MSDN -
Hi all, ComboBox.SelectedValue dont seem to work correctly. Code:
...
void SetupCustomers(){ //called from form constructor
cbCust.DataSource = dbase.GetCustomers(); //returns Customer[] from database
cbCust.DisplayMember = "Name"; // The name property
cbCust.ValueMember = "ID"; // The ID property (PK in database)
}
...However when calling
cbCust.SelectedValue
returns the wrong value. Calling((Customer)cbCust.SelectedItem).ID
gives the correct answer. This seems to be a problem with the DisplayMember being (automatically) sorted as all the returned SelectedValue's are within range. EG : Customers[] expanded may look like this: ID(Value) Name(Display) 20 Ben 21 John 23 Ann 26 Frank 27 Arnold The combobox will display the Displaymembers in alphebetical order. Here is where the problem lies (i think :)): Value Display 20 Ann 21 Arnold 23 Ben 26 Frank 27 John Now this is how Mr. ComboBox display and intepret Value and Display members, thus getting the mappings all wrong. If anyone has had a similar experience or can spot where I made a mistake, please tell me :) Thanking all replies in advance Cheers :) READ MSDNI don't suppose you're setting the Sort property on the combobox? If not maybe an explicit set to false would do the trick
-
I don't suppose you're setting the Sort property on the combobox? If not maybe an explicit set to false would do the trick
-
Thanx Chris, however... 1. no the Sort property is not set. 2. but Sorting is really needed. :) Like i said a quick work around does the trick, but i have searched forums and articles with nothing pointing to what i experience. READ MSDN
I was able to reproduce the behavior doing to following: I made a class: test with properties Name and Id. Set the ComboBox's DataSource to a test[] With Name as DisplayMember and Id as ValueMember Set Sorted=true Noting that I had to set sort before setting the DataSource property otherwise it will throw an exception. In thinking about it the behavior makes sense that it could happen this way. When you set the DataSource of a ComboBox it copies the list to its own internal list. Text returns the item in the internal list. SelectedValue uses a combiniation of CurrenctManager ,SelectedIndex and ValueMember to retrieve an Item from the datasource. SelectedItem is probably a pointer stored in the internal list that gets Sorted with Text. Anyways Sorting your DataSource (via Array.Sort, ArrayList.Sort, DataTable.DefaultView.Sort; whatever the type of DataSource you have) should fix the problem. Even if the Sorted property inadvertantly gets set on the combobox, there's no mismatch between your datasource and its internal list because its already sorted.:)
-
I was able to reproduce the behavior doing to following: I made a class: test with properties Name and Id. Set the ComboBox's DataSource to a test[] With Name as DisplayMember and Id as ValueMember Set Sorted=true Noting that I had to set sort before setting the DataSource property otherwise it will throw an exception. In thinking about it the behavior makes sense that it could happen this way. When you set the DataSource of a ComboBox it copies the list to its own internal list. Text returns the item in the internal list. SelectedValue uses a combiniation of CurrenctManager ,SelectedIndex and ValueMember to retrieve an Item from the datasource. SelectedItem is probably a pointer stored in the internal list that gets Sorted with Text. Anyways Sorting your DataSource (via Array.Sort, ArrayList.Sort, DataTable.DefaultView.Sort; whatever the type of DataSource you have) should fix the problem. Even if the Sorted property inadvertantly gets set on the combobox, there's no mismatch between your datasource and its internal list because its already sorted.:)