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

ComboBox binding to Items Collection

1 Answer 148 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.
Gary C
Top achievements
Rank 1
Gary C asked on 08 Dec 2009, 01:47 AM
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
        } 
 
    } 
 

1 Answer, 1 is accepted

Sort by
0
Victor
Telerik team
answered on 08 Dec 2009, 11:24 AM
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.
Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
Gary C
Top achievements
Rank 1
Answers by
Victor
Telerik team
Share this question
or