I cannot set the default combobox selected value for an unbound combobox. Here is my code:
System.Console.WriteLine("Current Tag Org Id = " + CurrentTag.Org.OrgId); ddlRUC.SelectedValue = CurrentTag.Org.OrgId; System.Console.WriteLine("ddlRUC selected value = " + ddlRUC.SelectedValue);
Here is the output: Current Tag Org Id = 285 ddlRUC selected value =
Note that ddlRUC.SelectedValue has not been set to 285. Does the datasource need to be bound in order to use the SelectedValue property? If so, how do I set the default item shown in a combobox that is not bound?
A combobox (like a Listbox) has 2 mechanisms for dealing with the selection. Either:
You assign a List to the DataSource property and set the ValueMember and DisplayMember to the names of properties of items of that list. Or,
You fill the Items property with
objects of your choice, the ToString()
will be displayed.
In scenario 1) you can use SelectedValue to get/set the selection based on the ValueMember.
in scenario 2) you use SelectedItem property instead of SelectedValue
So the question is, how do you fill the Items?
The SelectedValue property will only work for a databound listbox. If you can create your list items in a List<>, you can then bind the list to the control and SelectedValue will work as you would like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With