我有这种文本框用于搜索数据库中的数据。我尝试使用AutoComplete,但由于某种原因,它不起作用。我尝试了不同的变化但是对我来说没有任何效果。
private void AutoCompleteSearch()
{
AutoCompleteStringCollection stringCollection = new AutoCompleteStringCollection();
MySqlDataReader myReader;
MySqlConnection conn = new MySqlConnection(constring);
MySqlCommand commands = new MySqlCommand("SELECT ItemCode FROM addinv_table", conn);
conn.Open();
myReader = commands.ExecuteReader();
if (myReader.HasRows == true)
{
while(myReader.Read())
{
stringCollection.Add(myReader["ItemCode"].ToString());
}
}
myReader.Close();
textBox2.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox2.AutoCompleteCustomSource = stringCollection;
}
请记住,每次加载表单时都会调用AutoCompleteSearch()。