This is a migrated thread and some comments may be shown as answers.

radAutoCompleteBox with duplicate DisplayMembers

3 Answers 158 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Sebastien
Top achievements
Rank 1
Sebastien asked on 11 May 2016, 12:40 PM

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.

 

 

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 16 May 2016, 07:34 AM
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.
0
Sebastien
Top achievements
Rank 1
answered on 17 May 2016, 03:03 PM

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

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 19 May 2016, 08:02 AM
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.
Tags
General Discussions
Asked by
Sebastien
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Sebastien
Top achievements
Rank 1
Share this question
or