Windows.Form ComboBox Cannot Set SelectedValue Property for Unbound Control

I cannot set the default combobox 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 result: Current Org tag Id = 285 Selected value ddlRUC =

Note that ddlRUC.SelectedValue is not set to 285. Do I need to bind a data source to use the SelectedValue property? If so, how can I set the default item specified in a combo box that is not related?

+3
source share
7 answers

The SelectedValue property will work only for a list of data. If you can create your list items in List <>, you can bind this list to the control, and SelectedValue will work as you would like.

+11
source

Combobox (e.g. Listbox) has 2 mechanisms for selection. Or:

  • You assign a List to a DataSource and set the ValueMember and DisplayMember to the property names of the items in this list. Or,

  • You populate the Items property with objects of your choice that ToString() will be displayed.

In scenario 1), you can use SelectedValue to get / set the selection based on ValueMember.

in script 2) you use the SelectedItem property instead of SelectedValue

, , ?

+11

, , , , SelectedValue, . int32 vs int16. , , , . . int, , ! , combobox.

+4

ComboBox 286 ? .

0

, , , ComboBox ( , .Items, ListBox), , .

0

? Items.FindByText( ) Items.FindByValue( ), ListItem, .

0

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 claims to represent the property name of the object in the collection that is assigned to the DataSource property.

So yes, ValueMember only works with a data source.

0
source

Source: https://habr.com/ru/post/1710286/


All Articles