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

When is the databinding process complete

1 Answer 120 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Ervis
Top achievements
Rank 1
Ervis asked on 20 Dec 2012, 05:01 PM
Hi

I have simple scenario in which a RadDropDownList  is populated from a small table in a database. After it has been populated if needed I need to select the Item or index or display value programmatically. 
Also there is no problem with diplaying the items the items and the selection process works fine, items are diplayed in order.

I have tried to set all the fields that I can with respect to the databinding
here is the windows autogen code for the form where the dropdown resides
//
          // MachineTypeDropDownList
          //
          this.MachineTypeDropDownList.AutoCompleteDisplayMember = "M_Type";
          this.MachineTypeDropDownList.AutoCompleteValueMember = "MachineID";
          this.MachineTypeDropDownList.DataBindings.Add(new System.Windows.Forms.Binding("SelectedIndex", this.machineDataBindingSource, "MachineID", true));
          this.MachineTypeDropDownList.DataBindings.Add(new System.Windows.Forms.Binding("SelectedItem", this.machineDataBindingSource, "M_Name", true));
          this.MachineTypeDropDownList.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.machineDataBindingSource, "MachineID", true));
          this.MachineTypeDropDownList.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.machineDataBindingSource, "M_Name", true));
          this.MachineTypeDropDownList.DataSource = this.machineDataBindingSource;
          this.MachineTypeDropDownList.DisplayMember = "M_Type";
          this.MachineTypeDropDownList.Location = new System.Drawing.Point(182, 52);
          this.MachineTypeDropDownList.Name = "MachineTypeDropDownList";
          this.MachineTypeDropDownList.Size = new System.Drawing.Size(136, 20);
          this.MachineTypeDropDownList.TabIndex = 0;
          this.MachineTypeDropDownList.Text = "System.Data.DataViewManagerListItemTypeDescriptor";
          this.MachineTypeDropDownList.ValueMember = "MachineID";
          this.MachineTypeDropDownList.DataBindingComplete += new Telerik.WinControls.UI.ListBindingCompleteEventHandler(this.MachineTypeDropDownList_DataBindingComplete);
          this.MachineTypeDropDownList.Initialized += new System.EventHandler(this.MachineTypeDropDownList_Initialized);


In the form constructor  InitializeComponent() is called after that I try to set the value of selected item of dropdown.
at this point Items count is still 0 
this.MachineTypeDropDownList.DropDownListElement.Items.Count
or
this.MachineTypeDropDownList.Items.Count

i have tried to use the lisener for databound compeleted but items are still 0
private void MachineTypeDropDownList_DataBindingComplete(object sender, Telerik.WinControls.UI.ListBindingCompleteEventArgs e)
       {
           System.Diagnostics.Debug.WriteLine("this.MachineTypeDropDownList.DropDownListElement.Items  --------------------------------" + this.MachineTypeDropDownList.DropDownListElement.Items.Count);
           System.Diagnostics.Debug.WriteLine("this.MachineTypeDropDownList.Items  --------------------------------" + this.MachineTypeDropDownList.Items.Count);
       }

I get the same result after when i try
private void MachineTypeDropDownList_Initialized(object sender, EventArgs e){}

My question is at what point , or what event signals that the binding process is complete and further manipulation of the dropdownlist can comence?
Why is it that by the end of the form contructor code,  the binding has not happened yet.


Thank you very much for any pointers to understand the telerik framework better
Ervis

1 Answer, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 21 Dec 2012, 12:09 PM
Hello Ervis,

Thank you for writing.

I would like to suggest to set the RadDropDownList's BindingContext property before the data binding - it is necessary for the items creating. Usually the BindingContext is set when control is added to the form. 

For example:
this.MachineTypeDropDownList.BindingContext = new System.Windows.Forms.BindingContext();
this.MachineTypeDropDownList.DataSource = this.machineDataBindingSource;

For more information about BindingContext please refer to the following article - Winforms BindingContext

I hope this helps. Let me know if you have additional questions.

Greetings,
Peter
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Tags
DropDownList
Asked by
Ervis
Top achievements
Rank 1
Answers by
Peter
Telerik team
Share this question
or