==============
因为ListBox控件对象绑定到DataSet中的Table的相应字段,如果要更新ListBox中的内容,则应该首先更新DataSet中相应Table的内容。如下提供一种实现方法,代码如下所示。
private void Form1_Load(object sender, System.EventArgs e)
{
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\homeworks\\databases\\xtreme.mdb;";
OleDbConnection myConn = new OleDbConnection(connString);
myConn.Open();
string sqlString = "Select * from test";
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(sqlString,myConn);
myDataAdapter.Fill(myDataSet,"test");
myConn.Close();
// DataBinding
listBox1.DataSource = myDataSet;
listBox1.DataBindings.Add("Text",myDataSet,"test.name");
listBox1.DisplayMember = "test.name";
listBox1.ValueMember = "test.id";
}
private void button1_Click(object sender, System.EventArgs e)
{
//更新myDataSet中的table表test中的内容
myDataSet.Tables["test"].Clear();
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\homeworks\\databases\\xtreme.mdb;";
OleDbConnection myConn = new OleDbConnection(connString);
myConn.Open();
string sqlString = "Select * from test";
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(sqlString,myConn);
myDataAdapter.Fill(myDataSet,"test");
myConn.Close();
}