radAutoCompleteBox with duplicate DisplayMembers

4 posts, 0 answers
  1. Sebastien
    Sebastien avatar
    2 posts
    Member since:
    Mar 2016

    Posted 11 May Link to this post

    Hi,

    I have an issue with my radAutoCompleteBox :

    The displayMember is the name of the customer... but there is many duplicated names in my datasource. 

    There isn't any problems when user select a new item, the issue is when the autoCompleteBox is initialized.

     

    Example : 

    I have 3 customers :

    Id Name

    1 Smith

    2 Smith

    3 Smith

    If user select "Smith" with the ID 2 by the autoCompleteBox, there isn't any problems, the Value of "Smith" will be 2.

    But if i want to initialize  my autoCompleteBox with "Smith" (2), i can only do this (for me) : autoCompletebox.Text  = "Smith".

     

    So, the value of "Smith" will be 1 (and not 2) because autoCompleteBox will take the first value he find.

     

    Is there another way to add items to my autoCompleteBox (something like autoCompleteBox.Items.Add("Smith", 2)) ?

     

    I specify that duplicate rows are normal here, another column is displayed in the dropdownlist of the autoCompleteBox with the customer ID and other fields.

     

    Thank you for your time, i hope you have a solution to help me.

     

     

  2. Dess
    Admin
    Dess avatar
    1605 posts

    Posted 16 May Link to this post

    Hello Sebastien,

    Thank you for writing.

    By default, RadAutoCompleteBox performs searching of items considering only the text. Hence, when you have duplicated names, it is normal that the first one will be selected. In order to achieve the required functionality, you can use the following approach:
    public Form1()
    {
        InitializeComponent();
     
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Rows.Add(1, "Smith");
        dt.Rows.Add(2, "Smith");
        dt.Rows.Add(3, "Smith");
        dt.Rows.Add(4, "Brown");
     
        this.radAutoCompleteBox1.AutoCompleteDataSource = dt;
        this.radAutoCompleteBox1.AutoCompleteDisplayMember = "Name";
        this.radAutoCompleteBox1.AutoCompleteValueMember = "Id";
     
        this.radAutoCompleteBox1.ListElement.Tag = 2;
        this.radAutoCompleteBox1.Text = "Smith;";
    }
     
    private void radButton1_Click(object sender, EventArgs e)
    {
        Console.WriteLine(this.radAutoCompleteBox1.Items.Last().Value);
    }
     
    public class CustomAutoCompleteBox : RadAutoCompleteBox
    {
        protected override RadTextBoxControlElement CreateTextBoxElement()
        {
            return new CustomAutoCompleteBoxElement();
        }
        public override string ThemeClassName
        {
            get
            {
                return typeof(RadAutoCompleteBox).FullName;
            }
        }
    }
     
    public class CustomAutoCompleteBoxElement : RadAutoCompleteBoxElement
    {
        protected override RadTextBoxListElement CreateListElement()
        {
            return new CustomTextBoxListElement();
        }
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(RadAutoCompleteBoxElement);
            }
        }
    }
     
    public class CustomTextBoxListElement : RadTextBoxListElement
    {
        public override RadListDataItem Find(string text)
        {
            RadListDataItem item = this.SelectedItem;
     
            if (item != null && this.IsMatching(item.Text, text))
            {
                return item;
            }
     
            for (int index = 0; index < this.DataLayer.ListSource.Count; index++)
            {
                item = this.DataLayer.ListSource[index];
     
                if (this.IsMatching(item.Text, text) && this.Tag + "" == item.Value + "")
                {
                    return item;
                }
            }
     
            return null;
        }
    }

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

    Regards,
    Dess
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Sebastien
    Sebastien avatar
    2 posts
    Member since:
    Mar 2016

    Posted 17 May Link to this post

    Hello Dess,

     I just integrate your solution ... and it works perfectly !

    My autoCompleteBox could be initialize with more than 1 items, so I made few modifications :

    public override RadListDataItem Find(string text)
        {
            RadListDataItem item = this.SelectedItem;
     
            if (item != null && this.IsMatching(item.Text, text))
            {
                return item;
            }
     
            for (int index = 0; index < this.DataLayer.ListSource.Count; index++)
            {
                item = this.DataLayer.ListSource[index];
     
                foreach (string tag in this.Tag.ToString().Split(';'))
                {
                    if (this.IsMatching(item.Text, text) && tag + "" == item.Value + "")
                    {
                        return item;
                    }
                }
            }
     
            return null;
        }

    And also, i put those two lines before filling the dataSource, or values remain empty :

    this.radAutoCompleteBox1.ListElement.Tag = 2;
    this.radAutoCompleteBox1.Text = "Smith;";

     

    Thank you very much for your help,

    Regards.

    Sébastien

  5. Dess
    Admin
    Dess avatar
    1605 posts

    Posted 19 May Link to this post

    Hello Sebastien,

    Thank you for writing back. 

    I am glad that the suggested solution was useful for achieving your custom requirement.

    If you have any additional questions, please let me know.

    Regards,
    Dess
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top