Dropdownlist selected value

2 posts, 0 answers
  1. Moe
    Moe avatar
    36 posts
    Member since:
    Jan 2018

    Posted 31 Jul 2019 Link to this post

    Hi Admin,

    The dropdownlist of autocompletemode is suggest and dropdownlist of dropdownstyle is dropdown.

    I have two dropdownlist. First is Category and second details.

    First Category data are Green,Red and Orange.

    Second Details data are Avocado, Green apple, Green grape, Kiwi, Pear, Honeydew Melon, Raspberry, Strawberry, cherry, Orange, Papaya, Tangerines, Pumpkin and Plum.

    When I key in  detail dropdownlist start from 'P' and I choose 'Plum' and Category dropdownlist auto change to Red  but  second dropdownlist automatically choose Strawberry. 

    If Category choose blank , all dropdownlist reset. If Category choose green, detail dropdownlist need to show green category code of fruit. No need to show all value.

     

    Below my testing coding.

    Imports Telerik.WinControls.UI

    Public Class RadForm1
        Property FolderCodeToUse As Nullable(Of Integer)
        Property DocumentTypeCodeToUse As Nullable(Of Integer)
        Property DocumentDescriptionToUse As String
        Property BlnSuppressFolderCode As Boolean
        Property BlnSuppressDocumentTypeCode As Boolean
        Property font As New Font("Arial", 14)
        Property ListOfDocumentTypes As List(Of DocumentTypes)
        Private Sub RadForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListOfDocumentTypes = New List(Of DocumentTypes)
            Dim lstfoldercode As New List(Of DocumentTypes)
            Dim fold1 As New DocumentTypes
            fold1.FolderCode = 1
            fold1.FolderNameText = "Green"
            lstfoldercode.Add(fold1)
            fold1 = New DocumentTypes
            fold1.FolderCode = 2
            fold1.FolderNameText = "Red"
            lstfoldercode.Add(fold1)
            fold1 = New DocumentTypes
            fold1.FolderCode = 3
            fold1.FolderNameText = "Orange"
            lstfoldercode.Add(fold1)

            lstfoldercode.Insert(0, New DocumentTypes)

            Dim doc1 As New DocumentTypes
            doc1.FolderCode = 1
            doc1.FolderNameText = "Green"
            doc1.DocumentTypeCode = 1
            doc1.DocumentTypeDescription = "Avocado"
            ListOfDocumentTypes.Add(doc1)

            doc1 = New DocumentTypes
            doc1.FolderCode = 1
            doc1.FolderNameText = "Green"
            doc1.DocumentTypeCode = 2
            doc1.DocumentTypeDescription = "Green apple"
            ListOfDocumentTypes.Add(doc1)

            doc1 = New DocumentTypes
            doc1.FolderCode = 1
            doc1.FolderNameText = "Green"
            doc1.DocumentTypeCode = 2
            doc1.DocumentTypeDescription = "Green grape"
            ListOfDocumentTypes.Add(doc1)

            doc1 = New DocumentTypes
            doc1.FolderCode = 1
            doc1.FolderNameText = "Green"
            doc1.DocumentTypeCode = 3
            doc1.DocumentTypeDescription = "Kiwi"
            ListOfDocumentTypes.Add(doc1)

            doc1 = New DocumentTypes
            doc1.FolderCode = 1
            doc1.FolderNameText = "Green"
            doc1.DocumentTypeCode = 4
            doc1.DocumentTypeDescription = "Pear"
            ListOfDocumentTypes.Add(doc1)

            doc1 = New DocumentTypes
            doc1.FolderCode = 1
            doc1.FolderNameText = "Green"
            doc1.DocumentTypeCode = 5
            doc1.DocumentTypeDescription = "Honeydew Melon"
            ListOfDocumentTypes.Add(doc1)

            doc1 = New DocumentTypes
            doc1.FolderCode = 2
            doc1.FolderNameText = "Red"
            doc1.DocumentTypeCode = 1
            doc1.DocumentTypeDescription = "Raspberry"
            ListOfDocumentTypes.Add(doc1)

            doc1 = New DocumentTypes
            doc1.FolderCode = 2
            doc1.FolderNameText = "Red"
            doc1.DocumentTypeCode = 6
            doc1.DocumentTypeDescription = "Strawberry"
            ListOfDocumentTypes.Add(doc1)
            doc1 = New DocumentTypes
            doc1.FolderCode = 2
            doc1.FolderNameText = "Red"
            doc1.DocumentTypeCode = 7
            doc1.DocumentTypeDescription = "Cherry"
            ListOfDocumentTypes.Add(doc1)

            doc1 = New DocumentTypes
            doc1.FolderCode = 3
            doc1.FolderNameText = "Orange"
            doc1.DocumentTypeCode = 8
            doc1.DocumentTypeDescription = "Orange"
            ListOfDocumentTypes.Add(doc1)

            doc1 = New DocumentTypes
            doc1.FolderCode = 3
            doc1.FolderNameText = "Orange"
            doc1.DocumentTypeCode = 9
            doc1.DocumentTypeDescription = "Papaya"
            ListOfDocumentTypes.Add(doc1)

            doc1 = New DocumentTypes
            doc1.FolderCode = 3
            doc1.FolderNameText = "Orange"
            doc1.DocumentTypeCode = 10
            doc1.DocumentTypeDescription = "Tangerines"
            ListOfDocumentTypes.Add(doc1)

            doc1 = New DocumentTypes
            doc1.FolderCode = 3
            doc1.FolderNameText = "Orange"
            doc1.DocumentTypeCode = 11
            doc1.DocumentTypeDescription = "Pumpkin"
            ListOfDocumentTypes.Add(doc1)

            doc1 = New DocumentTypes
            doc1.FolderCode = 2
            doc1.FolderNameText = "Red"
            doc1.DocumentTypeCode = 12
            doc1.DocumentTypeDescription = "Plum"
            ListOfDocumentTypes.Add(doc1)

            ListOfDocumentTypes.Insert(0, New DocumentTypes)

            Me.RadDropDownList2.ValueMember = "DocumentTypeCode"
            Me.RadDropDownList2.DisplayMember = "DocumentTypeDescription"
            Me.BlnSuppressDocumentTypeCode = True
            Me.RadDropDownList2.DataSource = Me.ListOfDocumentTypes.ToList
            Me.BlnSuppressDocumentTypeCode = False
            Me.RadDropDownList2.Refresh()
            For Each item As RadListDataItem In RadDropDownList1.ListElement.Items
                item.Font = font
            Next

            Me.RadDropDownList1.ValueMember = "FolderCode"
            Me.RadDropDownList1.DisplayMember = "FolderNameText"
            Me.BlnSuppressFolderCode = True
            Me.RadDropDownList1.DataSource = lstfoldercode.ToList
            Me.BlnSuppressFolderCode = False
            Me.RadDropDownList1.Refresh()
            For Each item As RadListDataItem In RadDropDownList1.ListElement.Items
                item.Font = font
            Next
        End Sub

        Private Sub RadDropDownList1_SelectedValueChanged(sender As Object, e As EventArgs) Handles RadDropDownList1.SelectedValueChanged
            Try
                If Me.BlnSuppressFolderCode Then
                    Exit Sub
                End If

                Dim CategoryCode As Integer = CInt(RadDropDownList1.SelectedValue)

                RadDropDownList1.DataSource = Nothing
                RadDropDownList1.Refresh()


                LoadDetailBasedOnCategory(CategoryCode)

            Catch ex As Exception

            End Try
        End Sub

        Private Sub RadDropDownList2_SelectedValueChanged(sender As Object, e As EventArgs) Handles RadDropDownList2.SelectedValueChanged
            Try
                If Me.BlnSuppressDocumentTypeCode Then
                    Exit Sub
                End If

                Dim detailstypeCode As Integer = CInt(RadDropDownList2.SelectedValue)

                If detailstypeCode = 0 Then
                    Exit Sub
                End If

                Dim categoryCodeSelected As Integer = CInt(RadDropDownList1.SelectedValue)
                If categoryCodeSelected = 0 Then

                    'STEP 5. Find the FolderCode based on the DocumentTypeCode selected
                    For Each detailTypes1 As DocumentTypes In Me.ListOfDocumentTypes
                        If detailTypes1.DocumentTypeCode = detailstypeCode Then
                            categoryCodeSelected = detailTypes1.FolderCode
                            Exit For
                        End If
                    Next

                    Me.DocumentTypeCodeToUse = detailstypeCode

                    Me.BlnSuppressFolderCode = True
                    RadDropDownList1.SelectedValue = categoryCodeSelected
                    Me.BlnSuppressFolderCode = False

                    LoadDetailBasedOnCategory(categoryCodeSelected)

                    Me.BlnSuppressDocumentTypeCode = True
                    RadDropDownList2.SelectedValue = Me.DocumentTypeCodeToUse.Value
                    Me.BlnSuppressDocumentTypeCode = False

                    Me.DocumentTypeCodeToUse = New Nullable(Of Integer)

                End If

            Catch ex As Exception

            End Try
        End Sub
        Private Sub LoadDetailBasedOnCategory(folderCodeSelected As Integer)
            Try
                'STEP 1. Get the list of DocumentTypeCode based on the FolderCode
                Dim listOfDocumentTypesToShow As New List(Of DocumentTypes)
                If folderCodeSelected = 0 Then
                    For Each documentTypes1 As DocumentTypes In Me.ListOfDocumentTypes.ToList
                        listOfDocumentTypesToShow.Add(documentTypes1)
                    Next
                ElseIf folderCodeSelected > 0 Then
                    For Each documentTypes1 As DocumentTypes In Me.ListOfDocumentTypes.ToList
                        If documentTypes1.FolderCode = folderCodeSelected Then
                            listOfDocumentTypesToShow.Add(documentTypes1)
                        End If
                    Next
                End If

                'STEP 2. Sort
                If listOfDocumentTypesToShow.Count >= 1 Then
                    listOfDocumentTypesToShow.Sort(Function(x, y)
                                                       Dim compare As Integer = 0
                                                       If Not String.IsNullOrEmpty(x.DocumentTypeDescription) AndAlso Not String.IsNullOrEmpty(y.DocumentTypeDescription) Then
                                                           compare = x.DocumentTypeDescription.CompareTo(y.DocumentTypeDescription)
                                                       End If
                                                       Return compare
                                                   End Function)

                    'STEP 3. Insert blank first row
                    listOfDocumentTypesToShow.Insert(0, New DocumentTypes)
                End If

                'STEP 4. Load data into dropdownlist (suppress any event handler)
                RadDropDownList2.ValueMember = "DocumentTypeCode"
                RadDropDownList2.DisplayMember = "DocumentTypeDescription"
                Me.BlnSuppressDocumentTypeCode = True
                RadDropDownList2.DataSource = listOfDocumentTypesToShow.ToList
                Me.BlnSuppressDocumentTypeCode = False
                RadDropDownList2.Refresh()
                For Each ListItem In RadDropDownList2.Items
                    ListItem.Font = font
                Next


            Catch ex As Exception

            End Try
        End Sub
    End Class
    Public Class DocumentTypes
        Property DocumentTypeCode As Integer
        Property FolderCode As Integer
        Property FolderNameText As String
        Property DocumentTypeDescription As String
    End Class

    Thanks

    Moe

     

  2. Todor Vyagov
    Admin
    Todor Vyagov avatar
    46 posts

    Posted 05 Aug 2019 Link to this post

    Hello, Moe,

    The provided sample project is greatly appreciated.

    After a deep look into your code, I have found out that you are using an approach with two-way cascading combo boxes in combination with auto complete functionality. In this case when you type "Plu" in the Detail RadDropDownList the auto complete popup with suggestions is opened. Then, you select the first item in the pop-up("Plum"). This triggers the SelectedValueChanged event in RadDropDownList, where your logic is executed. You assign a value to the Category RadDropDownList and then, based on this value, you repopulate with items the Detail RadDropDownList. This all happens while the auto complete pop-up is trying to set SelectedIndex to the Detail RadDropDownList. When the pop-up was opened the selected index of "Plum" is 14, but after the rebind the total amount of items in Detail RadDropDownList is reduced to 5. Now, the pop-up tries to set SelectedIndex=14, but this is not valid anymore and that is why the last item is selected(the "Strawberry" item).

    I have invested some time and effort to find a solution for this case and most of the time this two-way cascaded combos were into a dead lock situation. Fortunately, I have managed to find a working solution for your case, when I moved your rebinding logic when Detail RadDropDownList loses the input focus(on the Leave event). Refer to the attached project
    An alternative solution is to use a one-way combo-box direction, as this will reduce the complexity of the code. For example: a car type drop down and a car model drop down - you cannot enter the model until the make field is empty. 

    I hope this helps. Should you have any other questions do not hesitate to ask.

    Regards,
    Todor Vyagov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top