RadDropdownList - Using SelectedItem.Key

3 posts, 0 answers
  1. Gone2TheDogs
    Gone2TheDogs avatar
    65 posts
    Member since:
    May 2016

    Posted 10 Oct 2016 Link to this post

    With Standard comboboxes, I would set up a Key and Value. When I try the same with the RadDropdownlist, I get an error, when I try to get the value for the SelectedItem.Key.

    The following code block is how I populate the RadDropdownlist. This works just fine. Notice line 10 and 11 where I set the members as Key and Value

    01.''' <summary>
    02.''' Populates the dropdown_ cust serv cntry.
    03.''' </summary>
    04.Private Sub PopulateDropdown_CustServCntry()
    06.    cboCustServ_Cntry.Items.Clear()
    08.    Try
    09.        cboCustServ_Cntry.DataSource = New BindingSource(dictCntry, Nothing)
    10.        cboCustServ_Cntry.DisplayMember = "Value"
    11.        cboCustServ_Cntry.ValueMember = "Key"
    13.        'Default to US
    14.        cboCustServ_Cntry.SelectedIndex = cboCustServ_Cntry.FindString("UNITED STATES"'United States code
    16.    Catch ex As Exception
    17.    End Try
    18.End Sub  'PopulateDropdown_CustServCntry


    When I enter the following code for the SelectedIndexChanged event, I get the error at line 07. Is there a way I can make this work with the radDropdownlist?

    01.Private Sub cboCustServ_Cntry_SelectedIndexChanged(sender As Object, e As Telerik.WinControls.UI.Data.PositionChangedEventArgs) Handles cboCustServ_Cntry.SelectedIndexChanged
    02.    'need to set up State dropdown based on selected country
    03.    Dim CntryCode As String = ""
    05.    Try
    06.        Try
    07.            CntryCode = cboCustServ_Cntry.SelectedItem.Key
    08.        Catch ex As InvalidCastException
    10.        End Try
    13.        If Not IsNothing(CntryCode) AndAlso CntryCode.Trim <> "" Then
    14.            PopulateDropdown_CustServState(CntryCode)
    15.            cboCustServ_State.SelectedIndex = -1
    16.        End If
    18.    Catch ex As Exception
    20.    End Try
    21.End Sub  'cboCustServ_Cntry_SelectedIndexChanged


    I appreciate your input!



  2. Gone2TheDogs
    Gone2TheDogs avatar
    65 posts
    Member since:
    May 2016

    Posted 10 Oct 2016 in reply to Gone2TheDogs Link to this post

    The selectedItem.Value is the Key. So, I'm using this to get the value from the dictionary. I would edit my original post as "resolved", but I don't see an edit option in this forum layout.
  3. Dimitar
    Dimitar avatar
    2907 posts

    Posted 11 Oct 2016 Link to this post

    Hello Bob,

    Using the selected value is one way to retrieve this. You can use the SelectedItem as well, however, you need to cast the DataBoundItem to your business object, Here is a complete example:
    Partial Public Class RadForm1
        Inherits Telerik.WinControls.UI.RadForm
        Public Sub New()
            Dim data As New BindingList(Of MyObject)()
            For i As Integer = 0 To 9
                data.Add(New MyObject() With {
                    .Key = i,
                    .Value = "Item " & i
            Next i
            radDropDownList1.DataSource = data
            radDropDownList1.ValueMember = "Key"
            radDropDownList1.DisplayMember = "Value"
            AddHandler radDropDownList1.SelectedIndexChanged, AddressOf RadDropDownList1_SelectedIndexChanged
        End Sub
        Private Sub RadDropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.Data.PositionChangedEventArgs)
            Dim dataItem = TryCast(radDropDownList1.SelectedItem.DataBoundItem, MyObject)
            Dim value = dataItem.Value
            Dim key = dataItem.Key
        End Sub
    End Class
    Friend Class MyObject
        Public Property Key() As Integer
        Public Property Value() As String
    End Class

    Do not hesitate to contact us if you have other questions.
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top