C#搜索文本框自动完成任意字母 [英] C# search textbox autocomplete with any letter

查看:68
本文介绍了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


这篇关于C#搜索文本框自动完成任意字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆