New to Telerik UI for WinFormsStart a free 30-day trial

How to AutoComplete Items Considering the Description Text in RadDropDownList

Updated over 6 months ago

Environment

Product VersionProductAuthor
2020.1.113RadDropDownList for WinFormsDesislava Yordanova

Description

By default, the auto-complete behavior considers the Text property of the items in RadDropDownList and the these ones that match the filter criteria are shown in a separate pop up. This tutorial demonstrates how to implement auto-complete functionality that searches in the description text as well when using DescriptionTextListDataItems.

dropdownlist-autcomplete-in-description-text.png

Solution

Let's consider that the RadDropDownList control is programmatically populated with DescriptionTextListDataItems iterating the records in the Northwind.Customers table. Then, the AutoCompleteMode property is set to Suggest.

In order to control how the auto-complete items are filtered, it is necessary to create a derivative of the AutoCompleteSuggestHelper class and override its DefaultFilter method. It expects a boolean result indicating that the passed item matches the filter criteria or not. If a DescriptionTextListDataItem is passed to the method, we will consider the DescriptionText as well.

We will override the SyncItemsCore method as well in order to replace the default items in the auto-complete pop up with DescriptionTextListDataItems.

A sample implementation can be found in the following code snippet:

C#

private void RadForm1_Load(object sender, EventArgs e)
{
    this.customersTableAdapter.Fill(this.nwindDataSet.Customers);

    foreach (NwindDataSet.CustomersRow c in this.nwindDataSet.Customers)
    {
        DescriptionTextListDataItem descriptionItem = new DescriptionTextListDataItem();
        descriptionItem.Text = c.ContactName;
        descriptionItem.DescriptionText = c.ContactTitle;
        this.radDropDownList1.Items.Add(descriptionItem);
    }

    this.radDropDownList1.AutoCompleteMode = AutoCompleteMode.Suggest;

    this.radDropDownList1.DropDownListElement.AutoCompleteSuggest = 
        new CustomAutoCompleteSuggestHelper(this.radDropDownList1.DropDownListElement);
}

public class CustomAutoCompleteSuggestHelper : AutoCompleteSuggestHelper
{
    public CustomAutoCompleteSuggestHelper(RadDropDownListElement owner)
        : base(owner)
    {
    }

    //replace the deafult autocomplete items with DescriptionTextListDataItems
    protected override void SyncItemsCore()
    {
        this.DropDownList.ListElement.Items.Clear();
        this.DropDownList.ListElement.BeginUpdate();
        foreach (DescriptionTextListDataItem item in Owner.Items)
        {
            DescriptionTextListDataItem newItem = new DescriptionTextListDataItem();
            newItem.Text = item.Text;
            newItem.DescriptionText = item.DescriptionText; 
            newItem.Value = item.Value;
            newItem.TextWrap = item.TextWrap;
            newItem.Enabled = item.Enabled; 
            this.DropDownList.ListElement.Items.Add(newItem);
        }

        this.DropDownList.ListElement.EndUpdate();
    }

    protected override bool DefaultFilter(RadListDataItem item)
    {
        DescriptionTextListDataItem descriptionItem = item as DescriptionTextListDataItem;

        if (descriptionItem != null)
        {
            bool matchesFilter= descriptionItem.Text.ToLower().Contains(this.Filter.ToLower()) ||
                   descriptionItem.DescriptionText.ToLower().Contains(this.Filter.ToLower());
            return matchesFilter;
        }

        return base.DefaultFilter(item);
    }
}

   
In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support