Setting DataSource property leaves an empty Items collection

3 posts, 1 answers
  1. IT Department
    IT Department avatar
    10 posts
    Member since:
    Apr 2010

    Posted 08 Jul 2014 Link to this post

    I have created a custom control wrapper for the DropDownList. My main reason was to create some shortcut methods for custom objects that we are using to populate the list.

    I am trying to write some automated tests for my custom control. The problem is that when I create an instance of the custom control in the automated test (but not actually put it on a form or control), some of the data functionality is lost. I can set the DataSource property (using a list of my custom objects), but the Items collection still shows a count of 0. I can set the SelectedIndex proprty to a valid value, but the SelectedItem property remains null. When I do this same thing with an actual control on a form, the Items collection is populated and reflects what I set in the DataSource property.

     I assume that the DropDownList is borrowing some functionality from the form to tie the DataSource and Items properties together.

    Is there something additional that I can do to make the DropDownList fully functional in an automated test, or is this not possible?

    I have tried using the Rebind method and this does not help. I also tried not using my custom control and just creating a RadDropDownList object directly in my automated test and it behaves the same way.
  2. Answer
    George avatar
    500 posts

    Posted 11 Jul 2014 Link to this post

    Hello Grant,

    Thank you for writing.

    The reason why you cannot bind RadDropDownList while not in a form is that it uses the BindingContext of the form to get the CurrencyManager which is used to parse the DataSource. Since all the controls are UI elements their purpose is to be added to a Form or a UserControl. If you want to use the DataSource property in such automated tests you should also create a form and add the DropDownList to the Form:
    Form f = new Form();
    RadDropDownList list = new RadDropDownList();

    Another way is to add items to the DropDownList directly using the ItemsCollection:
    RadDropDownList list = new RadDropDownList();
    for (int i = 0; i < 10; i++)
        list.Items.Add(new RadListDataItem("Text", "value"));

    I hope this helps.

    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
  3. IT Department
    IT Department avatar
    10 posts
    Member since:
    Apr 2010

    Posted 29 Jul 2014 in reply to George Link to this post

    This worked, thank you.
Back to Top