Assigning datasource and SelectedIndexChanged event

7 posts, 0 answers
  1. Jonathan Simmons
    Jonathan Simmons avatar
    108 posts
    Member since:
    Jun 2005

    Posted 15 Jul 2009 Link to this post

    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
  2. Boryana
    Admin
    Boryana avatar
    330 posts

    Posted 16 Jul 2009 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Cory
    Cory avatar
    5 posts
    Member since:
    Oct 2012

    Posted 22 Jul 2009 Link to this post

    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 :-)
  5. Jonathan Simmons
    Jonathan Simmons avatar
    108 posts
    Member since:
    Jun 2005

    Posted 24 Jul 2009 Link to this post

    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
  6. Boryana
    Admin
    Boryana avatar
    330 posts

    Posted 24 Jul 2009 Link to this post

    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.
  7. Mieke
    Mieke avatar
    4 posts
    Member since:
    Jan 2012

    Posted 05 Jun 2014 Link to this post

    When u assign a datatable to multiple DropDownlist objects, use .datasource =  New DataView(yourDataTableObject);

    This solves al the problems in.
  8. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 09 Jun 2014 Link to this post

    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.
     
Back to Top
UI for WinForms is Visual Studio 2017 Ready