Adding Object to ComboBox

4 posts, 0 answers
  1. Robert
    Robert avatar
    7 posts
    Member since:
    Jan 2008

    Posted 19 Feb 2008 Link to this post

    I have a class, Employee with two fields, EmployeeID (integer) and EmployeeName (string).  There is an override to the ToString() method to return just the employee name.

    I need to a number of instances of this class to a RadComboBox such that when a row is selected, I can obtain the selected employee id
  2. Nikolay
    Admin
    Nikolay avatar
    1804 posts

    Posted 20 Feb 2008 Link to this post

    Hello Robert,

    Thank you for writing.

    You can realize the scenario by binding RadComboBox, which supports binding to varous types of sources. Please, refer to our Editors >> RadComboBox >> Data Binding section in our Quick Start Framework for more information.

    In your case, you should add your custom objects of type Employee to a generic BindingList of type Employee. Then you should bind RadComboBox to that list, and set RadComboBox.DisplayMember and RadComboBox.ValueMember values to the the corresponding Name and Id properties. You can then obtain the Id of the selected instance from the SelectedValue property.

    If you need additional assistance, do not hesitate to contact me.

    Greetings,
    Nikolay
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Robert
    Robert avatar
    7 posts
    Member since:
    Jan 2008

    Posted 20 Feb 2008 Link to this post

    Nikolay:

    Thank you for responding.

    I often need more flexibility than using data-binding.

    The application is N-tier.  From the service layer, I receive a List<Employee> collection.  In some cases, I will need to first add an item to the combobox to allow the user to not select any employee (including changing from a selected employee to no employee).  In other cases, if there is more than one employee in the collection, I would first add an item to the combobox to allow the user to select all users.

    I would rather not alter the collection.  I would very much like to do is to optionally add any special items first and then to cycle through the collections of employee objects an add them.

    Thanks,

    Bob

  5. Nikolay
    Admin
    Nikolay avatar
    1804 posts

    Posted 22 Feb 2008 Link to this post

    Hi Robert,

    Thank you for getting back to me.

    Actually, RadComboBox provides the flexibility that you want.

    As your data is in the form of a List<Employee>, it will be best if you bind it to the RadComboBox. This will not alter the collection in any way. Furthermore, you will be able to add your custom items, with the custom logic for selecting 'all' and 'none' of the employees.

    Please take a look at the sample project I have attached. You will notice how a generic list is bound to the RadComboBox, but at the same time I add my custom RadComboBoxItems.

    I hope this helps. Please contact me again if I can be of further assistance.

    Greetings,
    Nikolay
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
Back to Top