DropDownList custom autocomplete

3 posts, 1 answers
  1. Aleksandr
    Aleksandr avatar
    7 posts
    Member since:
    Nov 2012

    Posted 10 Jun 2014 Link to this post

    Hello!
    I'm using RadDropDownList control in DropDown and SuggestAppend mode. I have a databound collection of customers (simple object with properties Id, Name, Phone, Email etc).

    I've figured out how to implement "contains" search with:
    ddlCustomers.DropDownListElement.AutoCompleteSuggest.SuggestMode = SuggestMode.Contains;

    But now I need to search not only by customer Name, but by Phone too (and maybe by Email in the future).
    I think that I need to implement my own suggest helper and inherit it from AutoCompleteSuggestHelper.
    Am I right? If so, which methods should I override? Or maybe there is more simple solution here?
    Thank you in advance.
  2. Answer
    Dess
    Admin
    Dess avatar
    1609 posts

    Posted 13 Jun 2014 Link to this post

    Hello Aleksandr,

    Thank you for writing.

    Here is a sample implementation of a custom AutoCompleteSuggestHelper. Overriding the DefaultFilter method gives you the option to achieve custom logic for filtering by two or more fields.

    As to the SyncItemsCore method, it is used only to assign the DataBoundItem to the auto-complete item's Tag property, which will be used for access to fields different than the DisplayMember/ValueMember:
    public Form1()
    {
        InitializeComponent();
     
        this.radDropDownList1.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown;
        this.radDropDownList1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        this.radDropDownList1.DropDownListElement.AutoCompleteSuggest.SuggestMode = SuggestMode.Contains;
    }
     
    private void Form1_Load(object sender, EventArgs e)
    {
        this.customersTableAdapter.Fill(this.nwindDataSet.Customers);
        this.radDropDownList1.DataSource = this.customersBindingSource;
        this.radDropDownList1.DisplayMember = "ContactName";
        this.radDropDownList1.ValueMember = "CustomerID";
    }
     
    public class CustomDropDownList : RadDropDownList
    {
        public override string ThemeClassName 
        {
            get
            {
                return typeof(RadDropDownList).FullName; 
            }
        }
     
        protected override RadDropDownListElement CreateDropDownListElement()
        {
            return new CustomDropDownListElement();
        }
    }
     
    public class CustomDropDownListElement : RadDropDownListElement
    {
        protected override Type ThemeEffectiveType    
        {
            get   
            {
                return typeof(RadDropDownListElement);    
            }
        }
     
        protected override AutoCompleteSuggestHelper CreateAutoCompleteSuggestHelper()
        {
            return new CustomAutoCompleteSuggestHelper(this);
        }
    }
     
    public class CustomAutoCompleteSuggestHelper : AutoCompleteSuggestHelper
    {
        public CustomAutoCompleteSuggestHelper(RadDropDownListElement owner) : base(owner)
        {
        }
     
        protected override bool DefaultFilter(RadListDataItem item)
        {
            DataRowView rowView = item.Tag as DataRowView;
            if (rowView != null)
            {
                DataRow curtomerRow = rowView.Row;
                string customerAdrress = curtomerRow["Address"].ToString();
                return item.Text.ToLower().StartsWith(this.Filter.ToLower()) &&
                       customerAdrress.ToLower().Contains(this.Filter.ToLower());
            }
     
            return base.DefaultFilter(item);
        }
     
        protected override void SyncItemsCore()
        {
            this.DropDownList.ListElement.Items.Clear();
            this.DropDownList.ListElement.BeginUpdate();
            foreach (RadListDataItem item in Owner.Items)
            {
                RadListDataItem newItem = new RadListDataItem(item.Text);
                newItem.Tag = item;
                newItem.Value = item.Value;
                newItem.TextWrap = item.TextWrap;
                newItem.Enabled = item.Enabled;
                newItem.Tag = item.DataBoundItem;
                this.DropDownList.ListElement.Items.Add(newItem);
            }
     
            this.DropDownList.ListElement.EndUpdate();
        }
     
        public override void AutoComplete(KeyPressEventArgs e)
        {
            base.AutoComplete(e);
            if (this.DropDownList.Items.Count > 0)
            {
                this.DropDownList.SelectedIndex = this.DropDownList.FindString(this.Filter);
            }
        }
    }

    Note that this is just an example and it may not cover all possible cases. Feel free to modify it on a way, which suits your requirement best.

    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Desislava
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Aleksandr
    Aleksandr avatar
    7 posts
    Member since:
    Nov 2012

    Posted 16 Jun 2014 Link to this post

    Thank you! This worked for me.
Back to Top