Home Forums WinForms controls Other WinForms controls Populate ComboBox

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #17966 |

    How do I populate a combobox?

    Sometimes, I want to populate the Combobox’s dropdown list with a list of items that I provide. Other times, I want to fill the dropdown list with data from a database table or query.

    What properties/methods are used to fill the Combobox control so that it displays a list of items from which to choose?

    Imported from legacy forums. Posted by AlphaGuys (had 2237 views)

    User (Old forums)
    Member
    Post count: 23064

    Follow up to my own post, now that I’ve found the answers.

    1) To populate a static list at design time:

    Click on ComboBox, and a dropdown saying “(New ComboBoxItem)” appears. Double-click on that to create new Item. Type in value in the “Text” field of the Properties screen.

    2) To populate a static list at run time:

    Use the Items.Add method of the control, like this:

    MyComboCtl.Items.Add (“MyValue1”)
    MyComboCtl.Items.Add (“MyValue2”)

    3) To populate a dynamic (data-bound) list:

    You can’t. According to Xceed’s technical support, the ComboBox “control was not designed to be data bound.” As a workaround, you can read the data yourself, then use Items.Add to build the control, something like this (some details on creating a data connection have been left out):

    myCmd = New SqlCommand(“SELECT <FieldName> FROM <TableName>”, myConnection)
    myReader = myCmd.ExecuteReader
    Do While myReader.Read
    MyComboCtl.Items.Add (myReader(<FieldName>)
    Loop

    Imported from legacy forums. Posted by AlphaGuys (had 3140 views)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.