Assigning multiple DropDownLists

16 posts, 2 answers
  1. Tim
    Tim avatar
    23 posts
    Member since:
    Nov 2010

    Posted 11 Jan 2011 Link to this post

    This is probably going to seem pretty easy to some of you but I haven't been able to determine what the cause of this problem is.  If I try to set a RadListDataItem object and add that object to multiple DropDownList controls it doesn't work.  For instance, I have a simple For/Next loop that enumerates a list of COM ports.  Seems to me that I should be able to do a simple loop and add the items as shown below but it doesn't work.  Only one of the DropDownLists gets the data.  I'm sure it has something to do with the way the control is handling the dataitem objects.  I'm hoping someone can explain it to me and give a work around other than creating two For/Next loops (one for each DropDownList).

    Thanks!

    For i As Integer = 0 To 9
        Dim oItem As New RadListDataItem
        oItem.Text = "COM" & i
        oItem.Value = "COM" & i
        rddReceiveCOM.Items.Add(oItem)
        rddSendCOM.Items.Add(oItem)
        oItem = Nothing
    Next
  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 11 Jan 2011 Link to this post

    Hi Tim,

    The reason why you are not able to add a RadListDataItem to more than one RadDropDownList is because each RadListDataItem must have an owner (and can only have one owner). Instead, just use your loop but populate with a string.
    For i As Integer = 0 To 9
        Me.RadDropDownList1.Items.Add("COM" & i.ToString()) ' Add to first Drop Down
        Me.RadDropDownList2.Items.Add("COM" & i.ToString()) ' Add to second Drop Down
    Next

    Hope that helps, but let me know if you have further questions
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Tim
    Tim avatar
    23 posts
    Member since:
    Nov 2010

    Posted 12 Jan 2011 Link to this post

    Yes that helps explain a lot.  Thanks for the help!!

  5. Tim
    Tim avatar
    23 posts
    Member since:
    Nov 2010

    Posted 12 Jan 2011 Link to this post

    One more quick question (now I remember why my first revision code didn't work).  When I tried using your code I get an error back telling me:

    Value of type 'String' cannot be converted to 'Telerik.WinControls.UI.RadListDataItem'

    I tried this but it didn't work (same error)

    RadDropDownList1.Items.Add(
    TryCast("COM" & i.ToString, RadListDataItem))

    Any ideas?

    Thanks
  6. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 12 Jan 2011 Link to this post

    Hello Tim,

    What version of the controls are you using? For reference I'm using the latest Q3 2010 SP1 version, and adding string values works fine. If you're not using the latest, I'd suggest upgrading. The alternative would be to add separate RadListDataItems for each of your drop downs.
    Hope that helps
    Richard
  7. Tim
    Tim avatar
    23 posts
    Member since:
    Nov 2010

    Posted 12 Jan 2011 Link to this post

    On this machine I'm using Q2 2010 SP1.  I didn't think there was that big of a difference between versions that are so close but I guess I'm wrong.  I'll have to update.  I've been avoiding it since everything has been working fine.

    Thanks again
  8. Tim
    Tim avatar
    23 posts
    Member since:
    Nov 2010

    Posted 12 Jan 2011 Link to this post

    I just finished reading the release notes on Q3 & Q3 SP1 and I didn't see any of the changes for the RadDropDownList included anything related to this.  I wonder if I'm doing something wrong...
  9. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 12 Jan 2011 Link to this post

    Hi Tim,

    When adding items to the RadDropDownList, you don't have to try and convert a string to a RadListDataItem. Just add the string as per my example above.
    Let me know if that helps
    Richard
  10. Tim
    Tim avatar
    23 posts
    Member since:
    Nov 2010

    Posted 12 Jan 2011 Link to this post

    Hi Richard,

    Sorry I should've done a better job explaining.  I did try your suggestion but got the "Value of type 'String' cannot be converted to 'Telerik.WinControls.UI.RadListDataItem'" error.  I also tried the command I listed with TryCast but received the same exact error message.

    -Tim
  11. Tim
    Tim avatar
    23 posts
    Member since:
    Nov 2010

    Posted 12 Jan 2011 Link to this post

    Here's a screenshot of what I'm seeing in VS2010.  I was trying to add with and without .ToString() which is why the two lines look different.  I only have the following import.  Not sure if I need anything else.  This is just a simple configuration form so there's nothing complicated getting in the way.

    Imports
    Telerik.WinControls.UI
  12. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 12 Jan 2011 Link to this post

    Hello Tim,

    Please can you confirm what version you are using? In the latest version you can add items either by adding a RadListDataItem or a String.
    Me.RadDropDownList1.Items.Add("String")
    // or
    Me.RadDropDownList2.Items.Add(New RadListDataItem("COM" & i.ToString()))
    Regards,
    Richard
  13. Tim
    Tim avatar
    23 posts
    Member since:
    Nov 2010

    Posted 13 Jan 2011 Link to this post

    Hi Richard,

    The version I'm trying this with is 2010.2.10.806.  I can update to the latest version but it's somewhat of a pain so I don't do it unless I have to.  I didn't see anything in the notes that would indicate the new version would work properly but I can certainly try it.

    -Tim
  14. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 13 Jan 2011 Link to this post

    Hi Tim.

    You can still use RadListDataItem, you just can't add the same RadListDataItem to both RadDropDownLists
    For i As Integer = 0 To 9
        Me.RadDropDownList1.Items.Add(New RadListDataItem("COM" & i.ToString()))  'Add to first Drop Down 
        Me.RadDropDownList2.Items.Add(New RadListDataItem("COM" & i.ToString()))  'Add to first Drop Down 
    Next
    Hope that helps
    Richard
  15. Tim
    Tim avatar
    23 posts
    Member since:
    Nov 2010

    Posted 13 Jan 2011 Link to this post

    Yes that works!  Thank you so much for the help.

    Just out of curiosity - is the reason I can't use your original suggestion because of the version?  The version I'm using isn't old so I don't think it's the problem.  I'm going to be updating to 2010 SP3 soon (I have to find the time to update all of my current projects).  I'll be sure to try your original idea again after upgrading but I'm curious to know why it didn't work now.  Any ideas?

    Thanks again!
    -Tim
  16. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 13 Jan 2011 Link to this post

    Hi,

    Glad that helped. I'm guessing that you don't have that overload because of your version, but to be honest, I can't remember when it was introduced and don't have an older version of the controls to try it with. But it's certainly worth upgrading as there are many new controls, enhancements and fixes in the latest version.

    All the best
    Richard
  17. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 14 Jan 2011 Link to this post

    Hi Tim,

    Thank you for writing.

    We have added an Add method with a string parameter in Q2 2010 SP release. So if you want to use this overload, you should update your assemblies. For now, I suggest using Richard's solution.

    If you have any other questions, do not hesitate to contact us.
    If you have any other questions, do not hesitate to contact us.

    Greetings,
    Peter
    the Telerik team
    Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
Back to Top
UI for WinForms is Visual Studio 2017 Ready