当您单击ComboBox的字段和自动所有值时,所有值都会加倍。一旦他们再次单击,它们再次被两倍。我该如何解决?
我的代码:
connection.Open();
MySqlCommand command = new MySqlCommand(service.selectOnlyWorkerName(), connection);
MySqlDataAdapter da = new MySqlDataAdapter(command);
using (DataTable dt = new DataTable())
{
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox1.Items.Add(dr["worker_name"]);
}
}
connection.Close();
您单击时一次又一次添加相同的数据,因此,单击时清除ComboBox,添加以下行:
combobox1.items.clear();
尝试以下代码:
comboBox1.Items.Clear();
connection.Open();
MySqlCommand command = new MySqlCommand(service.selectOnlyWorkerName(), connection);
MySqlDataAdapter da = new MySqlDataAdapter(command);
using (DataTable dt = new DataTable())
{
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox1.Items.Add(dr["worker_name"]);
}
}
connection.Close();
我认为您必须清除ComboBoxx
简单代码:
Comboboxname.Items.Clear();