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

Change Source AutoCompleteBox

6 Answers 63 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Cesar
Top achievements
Rank 1
Cesar asked on 06 Mar 2014, 12:08 PM
Hi I'm having a problem when I want change the DataSource of the control.

I want to filter in my collection depending on the first selected item, for example.

I have 3 DataTables: 1 - All People, 2 - Men, 3 - Women.
First will be charged with Table 1 - All and and depending on the first selection I make, I want to update the DataSource with 2 - Men or 3 - Women.

I encounter problems when I try to change it. I'm doing at the event SelectionChanged, I note that have selected an item and the text ends with the delimiter character, then analyze and select the text which will be my new table and to modify the datasource throws ArgumentOutOfRangeException.

private void rACBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (this.rACBox.Items.Count == 1 && this.rACBox.Text.EndsWith("$"))
    {
       if(this.IsMen(this.rACBox.Items[0].Value.ToString())
       {
           rACBox.AutoCompleteDataSource = new BindingSource(this.Men);
       }
       else
       {
           rACBox.AutoCompleteDataSource = new BindingSource(this.Women);
       }
    }
}

Thx
Regards.

6 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 11 Mar 2014, 08:54 AM
Hello Cesar,

Thank you for writing.

I have looked your code and it looks OK. The only possible reason for this exception is that you do not have any items added to the AutoCompleteBox. For example you can initialize the control like this:
radAutoCompleteBox1.Text = "Men; Women;";

Also using the information that you have provided so far I have created a small sample project. In it if you click remove the "Women" item and click the space button the drop down will contain items only from the men data table.

I hope this information helps. Should you have any other questions, I will be glad to assist you.

Regards,
Dimitar
Telerik

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

0
Cesar
Top achievements
Rank 1
answered on 11 Mar 2014, 11:29 AM
Hi, Thanks for your reply.

That solution is not worth me, I do not want to have to decide which list to use, I want it to be automatic.

For you see the problem, I attached a project with the exception.

https://dl.dropboxusercontent.com/u/21787064/Auto_Complete.zip

I also tried to store the text in a variable, set empty the
AutoCompleteBox, upload the new list and use the text of the variable in
the AutoCompleteBox. The result was equal.

I hope answer, thanks.

Regards
0
Cesar
Top achievements
Rank 1
answered on 11 Mar 2014, 11:31 AM
Hi, Thanks for your reply.

That solution is not worth me, I do not want to have to decide which list to use, I want it to be automatic.

For you see the problem, I attached a project with the exception.

https://dl.dropboxusercontent.com/u/21787064/Auto_Complete.zip

I also tried to store the text in a variable, set empty the AutoCompleteBox, upload the new list and use the text of the variable in the AutoCompleteBox. The result was equal.

I hope answer, thanks.

Regards
0
Dimitar
Telerik team
answered on 14 Mar 2014, 07:54 AM
Hi Cesar,

Thank you for writing back.

In this case instead of changing the data source you should populate the items manually. This is necessary because when the data source is changed the view element children are cleared (in this case this is the first item). For example you can create a method that retrieves the items from particular data table and set the items accordingly:
private void Form1_Load(object sender, EventArgs e)
{
 
    SetItems(this.All);
    this.Table = "All";
    this.radAutoCompleteBox1.SelectionChanged += radAutoCompleteBox1_SelectionChanged;
}
 
private void radAutoCompleteBox1_SelectionChanged(object sender, Telerik.WinControls.UI.SelectionChangedEventArgs e
{
    if (this.radAutoCompleteBox1.Items.Count == 1 && this.radAutoCompleteBox1.Text.EndsWith(";"))
    {
        string name = this.radAutoCompleteBox1.Items[0].Text;
         
        if (name == "Carl" || name == "Edward" || name == "Louis")
        {
            SetItems(this.Men);               
            this.Table = "Men";
        }
        else
        {
            SetItems(this.Women);
            this.Table = "Women";
        }
    }           
}
 
public void SetItems(DataTable table)
{
    radAutoCompleteBox1.AutoCompleteItems.Clear();
    foreach (DataRow item in table.Rows)
    {
        radAutoCompleteBox1.AutoCompleteItems.Add((string)item.ItemArray[0]);
    }
}

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

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
0
Cesar
Top achievements
Rank 1
answered on 14 Mar 2014, 01:49 PM
Thanks for the reply but that solution does not work for me. Each table can contain more than 3000 items. if I have to visit them all is not an optimal solution

Regards
0
Dimitar
Telerik team
answered on 18 Mar 2014, 04:14 PM
Hi Cesar,

Thank you for writing back.

I have further investigated this case, but still the data source cannot be changed this way. I don't consider the provided solution as a slower one, because in either way the items should be taken from the data source and added to the drop down. The only difference is that in this case this is done in the form's code rather than by the control itself. 

I hope this information helps. 

Regards,
Dimitar
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
Tags
AutoCompleteBox
Asked by
Cesar
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Cesar
Top achievements
Rank 1
Share this question
or