我使用来自 ms access 数据库的列表框数据源创建了一个 winform 应用程序。问题是当我单击更新按钮时,列表框没有得到更新。例如,我将“ITEM1”更改为 ITEM2”,它只是不工作,但数据库可以,因为我执行了一个将更新数据库的命令。我需要重新加载(重新打开)我的应用程序,然后到那时我会看到我的修改.我如何更新列表框?我以前读过一些关于这个问题的东西,但我不太清楚..我希望有人能帮助我..
private BindingList<PRODUCTLIST> _productlist;
public Form1()
{
InitializeComponent();
}
public class PRODUCTLIST
{
public string ID { get; set; }
public string ITEM { get; set; }
public string ITEM_DESC { get; set; }
}
private static OleDbConnection GetConnection()
{
OleDbConnection mdbConn = new OleDbConnection();
try
{
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=shop.mdb;Jet OLEDB:Database Password=xxxxx;";
mdbConn = new OleDbConnection(connectionString);
mdbConn.Open();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return mdbConn;
}
private void Form1_Load(object sender, EventArgs e)
{
// make ID textbox un editable
textBoxID.Enabled = false;
_productlist = new BindingList<PRODUCTLIST>();
string strSQL = "SELECT * FROM Item ORDER BY ITEM";
OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQL, GetConnection());
DataSet dtSet = new DataSet();
myCmd.Fill(dtSet, "Item");
DataTable dTable = dtSet.Tables[0];
foreach (DataRow dtRow in dTable.Rows)
{
_productlist.Add(new PRODUCTLIST() { ID = dtRow["ID"].ToString(), ITEM = dtRow["ITEM"].ToString(), ITEM_DESC = dtRow["ITEM_DESC"].ToString() });
}
listBox1.DisplayMember = "ITEM";
listBox1.DataSource = _productlist;
//listBox1.DataBindings = _productlist;
listBox1.ValueMember = "ID";
// set the textbox binding and DataSourceUpdateMode to Never because i have other button to add the new item
textBoxID.DataBindings.Add("Text", _productlist, "ID", false, DataSourceUpdateMode.Never);
textBoxITEM.DataBindings.Add("Text", _productlist, "ITEM", false, DataSourceUpdateMode.Never);
textBoxITEMDESC.DataBindings.Add("Text", _productlist, "ITEM_DESC", false, DataSourceUpdateMode.Never);
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
// update data on database
OleDbCommand cmd = new OleDbCommand("UPDATE Item SET ITEM = @ITEM, ITEM_DESC = @ITEM_DESC WHERE ID = @ID", GetConnection());
cmd.Parameters.AddWithValue("@ITEM", textBoxITEM.Text);
cmd.Parameters.AddWithValue("@ITEM_DESC", textBoxITEMDESC.Text);
cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(textBoxID.Text));
cmd.ExecuteNonQuery();
// make the listbox datasource update
//listBox1.Refresh();
//listBox1.DataSource = null;
//listBox1.DataSource = _productlist;
//_productlist = new BindingList<PRODUCTLIST>();
//string strSQL = "SELECT * FROM Item ORDER BY ITEM";
//OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQL, GetConnection());
//DataSet dtSet = new DataSet();
//myCmd.Fill(dtSet, "Item");
//DataTable dTable = dtSet.Tables[0];
//foreach (DataRow dtRow in dTable.Rows)
//{
// _productlist.Add(new PRODUCTLIST() { ID = dtRow["ID"].ToString(), ITEM = dtRow["ITEM"].ToString(), ITEM_DESC = dtRow["ITEM_DESC"].ToString() });
//}
//listBox1.DisplayMember = "ITEM";
//listBox1.DataSource = _productlist;
////listBox1.DataBindings = _productlist;
//listBox1.ValueMember = "ID";
}