ComboBox binding to Items Collection

2 posts, 0 answers
  1. Gary C
    Gary C avatar
    8 posts
    Member since:
    Nov 2008

    Posted 07 Dec 2009 Link to this post

    Hello,

    We are upgrading to the latest version of the WinForms components. I see there have been changes to the binding logic. I have a RadComboBox and I'm binding a list to it. If I create the combo dynamically and assign my list to the datasource property the Items collection is not bound (Items.Count = 0) until I assign the combo box to a container.

    Is there a way to force this sooner without requiring a parent component? In this case our combo box is in a grid with an image associated with each item in the list so we are doing some initialization and need the items to be there for this.

    Thanks,

    -Gary

    Here is some code to demonstrate the problem:
        public partial class Form1 : Form 
        { 
            const string DISPLAY_FIELD = "Name"
            const string VALUE_FIELD = "ID"
            public Form1() 
            { 
                InitializeComponent(); 
            } 
     
            private void radButton2_Click(object sender, EventArgs e) 
            { 
                List<ComboItems> comboItems = new List<ComboItems>(3); 
     
                comboItems.Add(new ComboItems("Red", 1)); 
                comboItems.Add(new ComboItems("White", 2)); 
                comboItems.Add(new ComboItems("Blue", 3)); 
     
                RadComboBox rcb = new RadComboBox(); 
                rcb.DisplayMember = DISPLAY_FIELD
                rcb.ValueMember = VALUE_FIELD
                rcb.DataSource = comboItems
     
                // rcb.Items.Count == 0 at this point 
                rcb.Parent = this
     
                // now rcb.Items.Count == 3  
            } 
        } 
     
        public class ComboItems 
        { 
            public string Name { get; set; } 
            public int ID { get; set; } 
     
            public ComboItems(string displayMember, int valueMember) 
            { 
                Name = displayMember
                ID = valueMember
            } 
     
        } 
     
  2. Victor
    Admin
    Victor avatar
    1351 posts

    Posted 08 Dec 2009 Link to this post

    Hello Gary C,

    Thank you for your question. The reason why RadComboBox is not being bound is that it does not have a BindingContext object until it is added to a container and the form on which the container resides is shown. You can force RadComboBox to bind itself when you assign a BindingContext explicitly, for example:

    rcb.BindingContext = new BindingContext();

    Please write again if you need further assistance.

    Regards,
    Victor
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top