本文介绍了C#中的自动完成框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个自动完成框.例如我写一个字符
上午" ,则应在文本框中建议所有数据.下面的代码在文本更改事件上触发:
I want to create a Autocomplete box. For example I write a character
''am'' then all data should be suggested in textbox. Below code fire on text change event:
string search = textBox1.Text + '%';
SqlConnection cn = new SqlConnection(objcommn.getebitobiDatabase);
SqlCommand cmd = new SqlCommand("select firstname + ' ' + lastname as fullname from user_catalog where firstname like '" + search + "' or lastname like '" + search + "' ", cn);
//sqldataadapter dadp = new sqldataadapter(cmd);
//datatable dt = new datatable();
//dadp.fill(dt);
cn.Open();
SqlDataReader dreader = cmd.ExecuteReader();
if (dreader.HasRows == true)
{
while (dreader.Read())
namesCollection.Add(dreader["fullname"].ToString());
}
else
{
MessageBox.Show("data not found");
} dreader.Close();
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = namesCollection;
请帮帮我.
谢谢.
Please help me.
Thanks.
推荐答案
这篇关于C#中的自动完成框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!