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

Assigning multiple DropDownLists

15 Answers 166 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 11 Jan 2011, 12:05 PM
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

15 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 11 Jan 2011, 12:16 PM
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
0
Tim
Top achievements
Rank 1
answered on 12 Jan 2011, 08:16 AM
Yes that helps explain a lot.  Thanks for the help!!

0
Tim
Top achievements
Rank 1
answered on 12 Jan 2011, 08:26 AM
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
0
Richard Slade
Top achievements
Rank 2
answered on 12 Jan 2011, 09:10 AM
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
0
Tim
Top achievements
Rank 1
answered on 12 Jan 2011, 09:40 AM
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
0
Tim
Top achievements
Rank 1
answered on 12 Jan 2011, 09:45 AM
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...
0
Richard Slade
Top achievements
Rank 2
answered on 12 Jan 2011, 10:08 AM
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
0
Tim
Top achievements
Rank 1
answered on 12 Jan 2011, 10:52 AM
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
0
Tim
Top achievements
Rank 1
answered on 12 Jan 2011, 10:58 AM
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
0
Richard Slade
Top achievements
Rank 2
answered on 12 Jan 2011, 11:00 AM
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
0
Tim
Top achievements
Rank 1
answered on 13 Jan 2011, 09:29 AM
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
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 13 Jan 2011, 10:22 AM
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
0
Tim
Top achievements
Rank 1
answered on 13 Jan 2011, 07:26 PM
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
0
Richard Slade
Top achievements
Rank 2
answered on 13 Jan 2011, 09:17 PM
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
0
Peter
Telerik team
answered on 14 Jan 2011, 09:56 AM
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.
Tags
DropDownList
Asked by
Tim
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Tim
Top achievements
Rank 1
Peter
Telerik team
Share this question
or