C#搜索文本框自动完成任意字母

问题描述:

我有一个textbox1,它的自动完成源是在数据库上..但它不能正常工作..例如:



我在桌子上这些名字:



美国

联合王国





并且用户开始输入王国没有任何建议只有在我从头开始输入时才出现英国



我需要建议如果出现的话我从中间或结束或开始打字



我尝试过:



i have a textbox1 and it's autocomplete source is on database .. but it's not working as it should .. as an example :

I have on my table these names :

united states
united kingdom


and the user start typing "kingdom" no suggestion appear only when I type it from the beginning "united kingdom"

I need the suggestion appear if I type starting from the middle or end or beginning

What I have tried:

cm = new SqlCommand();
cm.Connection = cn;
string searchFor = "%" + textBox1.Text + "%"; //the string the user entered.
AutoCompleteStringCollection namecollection = new AutoCompleteStringCollection();
cm.CommandText = @"SELECT distinct itmname AS name FROM table1 WHERE name LIKE @name";

cm.Parameters.AddWithValue("@name", searchFor);
SqlDataReader rea = cm.ExecuteReader();
if (rea.HasRows == true)
{
    while (rea.Read())
        namecollection.Add(rea["name"].ToString());
}
rea.Close();
autoCompleteTextbox1.AutoCompleteCustomSource = namecollection;

您好b $ b

您可以将整个数据带到数据表和使用linq查询获取包含您在文本框中输入的文本的结果





参考此链接



使用SubString搜索自动完成TextBox,类似于SQL Like或包含 [ ^ ]



谢谢
Hi
you can take the entire data to datatable and use a linq query to get results which contains the text you entered in textbox


refer this link

AutoComplete TextBox with SubString search, similar to SQL Like or Contains[^]

Thanks