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

Assigning datasource and SelectedIndexChanged event

6 Answers 236 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.
Jonathan
Top achievements
Rank 1
Jonathan asked on 15 Jul 2009, 11:45 AM
Hi, at last I am delving into the Winforms controls for the first time, but I am coming up against some strange behaviour when manually binding a RadComboBox to a datasource.

Here is the code called from form_load:

protected void PopulateDropDown()  
{  
DataTable topics = osa.GetAllTopicsForExistingAssessmentsDT();  
radComboBox1.DataSource = topics;  
radComboBox1.DisplayMember = topics.Columns["topictext"].ColumnName;  
radComboBox1.ValueMember = topics.Columns["topicID"].ColumnName;  

The datasource is simply a DataTable returned from a webservice.

The issue I have is the when DisplayMember is set, the radComboBox1_SelectedIndexChanged event fires twice and then once again when ValueMember is set. Why is this?

Regards,
Jonathan

Regards,
Jonathan

6 Answers, 1 is accepted

Sort by
0
Boryana
Telerik team
answered on 16 Jul 2009, 08:34 AM
Hi Jonathan Simmons,

Thank you for contacting us.

The SelectedIndexChanged event is fired when you are setting the DataSource property. The event triggers if you are setting the ValueMember and DisplayMember after you have assigned the DataSource property. The reason is that the list of items is created each time you are setting these properties. This is the default behavior of the radComboBox.
 
The only workaround that I can suggest is to use BindingSource and invoke the Fill method after setting the DataSource, DisplayMember and ValueMember properties of the radComboBox. Here is an example:

this.radComboBox1.DataSource = employeesBindingSource; 
this.radComboBox1.DisplayMember = "LastName"
this.radComboBox1.ValueMember = "EmployeeID"
this.employeesTableAdapter.Fill(this.nwindDataSet.Employees); 

Unfortunately, I was not able to produce a workaround for your issue, since you are using a DataTable. If you encounter any other issues, do not hesitate to contact me back.
 

Regards,
Boryana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Cory
Top achievements
Rank 1
answered on 23 Jul 2009, 02:52 AM
Hi Jonathan,

I found that when I was binding a DataSource, that it will generate "InvalidCastException was unhandled by user code" if I define the DataSource before the DisplayMemeber and ValueMemeber.

I don't know if this will assist you with your issue, but it's something I just came across recently and wanted to share :-)
0
Jonathan
Top achievements
Rank 1
answered on 24 Jul 2009, 07:21 AM
Thanks Cory.

The problem I was having was that selecting a row in one grid loads up child records in a second grid and selecting the first row in the parent grid happens when the radComboBox selected index changes.

When I was setting the datasource for the radComboBox, the radComboBox1_SelectedIndexChanged event fired 3 times during this process which was causing errors in the code which was populating the parent and child grids as the datasources for these grids changes depending on what is selected from the radComboBox.

I have managed to solve the issue of the SelectedIndexChanged event being fired when (I thought) it probably should not.

Thanks for your input.
Regards,
Jonathan
0
Boryana
Telerik team
answered on 24 Jul 2009, 12:36 PM
Hello everyone,

Jonathan, I am glad that you managed to find a solution of your issue.

Cory, thank you for sharing your experience. In order to address the issue, our team will need more details about your case. For example, you can send us a step-by-step tutorial that leads to the issue. Another options is to open a Support Ticket and send us a sample project that re-creates this exception. Thus, we will be able to detect what causes it. I am looking forward to your reply.

All the best,
Boryana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Mieke
Top achievements
Rank 1
answered on 05 Jun 2014, 06:26 AM
When u assign a datatable to multiple DropDownlist objects, use .datasource =  New DataView(yourDataTableObject);

This solves al the problems in.
0
Dimitar
Telerik team
answered on 09 Jun 2014, 01:02 PM
Hello Mieke,

Thank you for sharing your experience with the community. I think your suggestion is appropriate to this case. 

Do not hesitate to contact us if you have other questions.

Regards,
Dimitar
Telerik
 
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.
 
Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Boryana
Telerik team
Cory
Top achievements
Rank 1
Jonathan
Top achievements
Rank 1
Mieke
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or