我尝试使用以下代码来过滤组合框的成员,但不幸的是,在输入第一个字母组合框后,会自动将第一个相关成员分配给组合框,并且其文本将填充所选项目的显示成员。因此用户无法继续键入其他单词来搜索项目。
private void cmbCompany_TextChanged(object sender, EventArgs e)
{
string QueryCompany = string.Format("select id,title from acc.dl union select null , null order by title");
SqlDataAdapter DA1 = new SqlDataAdapter(QueryCompany, con);
con.Open();
DataTable DT1 = new DataTable();
DA1.Fill(DT1);
con.Close();
DataView dv = new DataView(DT1);
dv.RowFilter = "Title like '%" + cmbCompany.Text + "%'";
cmbCompany.DisplayMember = "Title";
cmbCompany.ValueMember = "id";
cmbCompany.DataSource = dv;
}
你能为这个问题提供任何解决方案吗?
答案 0 :(得分:1)
设置组合框的这些属性。
cmbCompany.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
cmbCompany.AutoCompleteSource = AutoCompleteSource.ListItems;
删除事件TextChanged
,并绑定datasource
combobox
一次(在加载或任何其他事件中)。
private void Form1_Load(object sender, EventArgs e)
{
string QueryCompany = string.Format("select id,title from acc.dl union select null , null order by title");
SqlDataAdapter DA1 = new SqlDataAdapter(QueryCompany, con);
con.Open();
DataTable DT1 = new DataTable();
DA1.Fill(DT1);
con.Close();
cmbCompany.DataSource = DT1;
cmbCompany.DisplayMember = "Title";
}
这应该符合您的期望。
答案 1 :(得分:0)
更改为
cmbCompany.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
cmbCompany.AutoCompleteSource = AutoCompleteSource.ListItems;