This is a migrated thread and some comments may be shown as answers.

Adding Object to ComboBox

3 Answers 119 Views
ComboBox and ListBox (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Robert
Top achievements
Rank 1
Robert asked on 19 Feb 2008, 09:29 PM
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

3 Answers, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 20 Feb 2008, 12:54 PM
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
0
Robert
Top achievements
Rank 1
answered on 20 Feb 2008, 05:20 PM

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

0
Nikolay
Telerik team
answered on 22 Feb 2008, 10:32 AM
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
Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
Robert
Top achievements
Rank 1
Answers by
Nikolay
Telerik team
Robert
Top achievements
Rank 1
Share this question
or