我已經閱讀了許多關於同樣的事情的其他問題,但似乎答案不屬於我如何執行我的代碼。在從用戶界面向我的數據庫添加或刪除一條記錄或一行信息後,它不會顯示在組合框中,直到我重新啓動應用程序。也許有人可以啓發我,因爲我對此有點新鮮。點擊添加按鈕時,以下是我的代碼。C#,添加數據後刷新組合框
private void FBinterface_Load(object sender, EventArgs e)
{
txtSerial.Focus();
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string SerialQuery = "select SerialNumber from Inventory";
command.CommandText = SerialQuery;
//TO READ DATA
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
comboSerial.Items.Add(reader["SerialNumber"]);
}
connection.Close();
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message);
connection.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtSerial.Text))
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = @"insert into
Inventory(SerialNumber,PartNumber,ROnumber,Location)
values ('" +
txtSerial.Text + "','" +
txtPart.Text + "','" +
txtRO.Text + "','" +
txtLocation.Text + "')";
//TO READ DATA
command.ExecuteNonQuery();
MessageBox.Show("Inventory Added");
txtPart.Clear();
txtSerial.Clear();
txtRO.Clear();
txtLocation.Clear();
if (dataGridFB.DataSource != null)
{
dataGridFB.DataSource = null;
}
else
{
dataGridFB.Rows.Clear();
}
txtSerial.Focus();
connection.Close(); // CLOSE HERE OR
// YOU CANNOT ENTER RECORDS SIMULTANEOUSLY
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message);
connection.Close();
}
}
}
這就是爲什麼我有這樣的方式從頁面中刪除它們。那麼我如何編輯我擁有的? – CamlCase
我沒有看到ComboBox代碼......你如何初始化它?您如何首先獲得數據源?你想添加/更改哪些數據? – Ian
我很想告訴你,但我不知道如何添加在您的文章 – CamlCase