在我的代碼中,我創建了一個表並將CSV文件導出到它。在我的代碼的另一部分,comboBox填充了我的數據庫中的表名。這是如下圖所示:在數據庫中創建新表後刷新組合框
private void FillCombo()
{
try
{
string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
using (SqlConnection con2 = new SqlConnection(connectionString))
{
con2.Open();
string query = "SELECT * FROM INFORMATION_SCHEMA.TABLES ";
SqlCommand cmd2 = new SqlCommand(query, con2);
SqlDataReader dr2 = cmd2.ExecuteReader();
while (dr2.Read())
{
int col = dr2.GetOrdinal("TABLE_NAME");
comboBox1.Items.Add(dr2[col].ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
什麼想實現是刷新組合框,以反映其通過上述功能執行新添加的表。什麼浮現在腦海首先是創建一個按鈕,並把上面的功能是這樣的:
private void button1_Click_1(object sender, EventArgs e)
{
//this is where I would just rewrite the contents of the above function
}
正如你猜到了,它的工作原理,但值會重複。如何在不復制現有信息的情況下刷新組合框?
清除項目先? – Berkay
如果你的意思'comboBox1.ResetText()'然後執行命令,然後同樣的結果,複製@Berkay – Jevon
他的意思'comboBox1.Items.Clear();' - 把它放在FillCombo'的'頂部,然後只需調用按鈕點擊處理程序中的「FillCombo」。 – Blorgbeard