tags:

views:

2231

answers:

5

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: 

Does your unbound ComboBox have 286 items in it? Otherwise that id won't mean anything to it.

orthod0ks
A: 

I may be misunderstanding exactly what you're trying to accomplish, but the ComboBox either has to have Items (I believe it has .Items, just like a ListBox) or it has to be bound to a datasource before there can be a default item.

AllenG
A: 

Do the items in your combobox have values? You could use Items.FindByText(string text) or Items.FindByValue(string value) to return the ListItem you are looking for.

Matthew Jones
A: 

The documentation for SelectedValue states that the property will return "an object containing the value of the member of the data source specified by the ValueMember property". The ValueMember property documentation states that it represents the name of an object property in the collection that is assigned to the DataSource property.

So yes, ValueMember works only together with a databound data source.

Fredrik Mörk
+3  A: 

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.

Scott Ewers