第一次单击时不更新列表框数据源
本文关键字:列表 数据源 更新 单击 第一次 | 更新日期: 2023-09-27 18:04:17
我创建了一个winform程序,从ms access数据库加载数据到我的列表框。当我点击我想要在列表框上更新数据库和数据源的项目并点击更新按钮时,该项目在第一次点击时不会更新,但在第二次点击时(在同一项目上),所以每次我需要修改我的数据时,我需要选择我的项目并单击更新按钮两次。这太疯狂了,哈哈。
private void buttonUpdate_Click(object sender, EventArgs e)
{
//update the database
OleDbDataAdapter cmd = new OleDbDataAdapter();
cmd.UpdateCommand = new OleDbCommand("UPDATE Item SET ITEM = @ITEM, ITEM_DESC = @ITEM_DESC WHERE ID = @ID",GetConnection());
cmd.UpdateCommand.Parameters.AddWithValue("@ITEM", textBoxITEM.Text);
cmd.UpdateCommand.Parameters.AddWithValue("@ITEM_DESC", textBoxITEMDESC.Text);
cmd.UpdateCommand.Parameters.AddWithValue("@ID", Convert.ToInt32(textBoxID.Text));
cmd.UpdateCommand.ExecuteNonQuery();
// update the datasource
_productlist.Clear();
listBox1.DataSource = null;
listBox1.Items.Clear();
Fill();
listBox1.Update();
}
发生的基本事情:
- 将数据加载到列表框并绑定到文本框
- 点击我的列表框上的项目并修改文本框 中的数据
- 点击更新按钮更新数据库和数据源
- 数据源第一次没有更新,但是数据库更新了
- 重复2
- 重复3号
- 数据源更新
我想要的:
- 将数据加载到列表框并绑定到文本框
- 点击我的列表框上的项目并修改文本框 中的数据
- 点击更新按钮更新数据库和数据源
- 数据源和数据库更新
我如何修复这个错误在我的应用程序??
我只是改变这个
// update the datasource
_productlist.Clear();
listBox1.DataSource = null;
listBox1.Items.Clear();
Fill();
listBox1.Update();
这个. . // update the datasource
_productlist.Clear();
listBox1.DataSource = null;
System.Threading.Thread.Sleep(500);
Fill();