我正在使用sqladapter
以直接從datagridview
更改數據庫值。.GetChanges始終顯示爲空
爲此我使用下面的代碼:
private void ok_Modify_Click(object sender, EventArgs e) {
//modify mod = new modify();
//modify_gv.DataSource = mod.TabletoMod(tickerBox.Text, FundBox.Text);
connetionString = Properties.Settings.Default.TraFra;
connection = new SqlConnection(connetionString);
Sql = "select * from Approval where Ticker ='" + tickerBox.Text + "'";
try {
connection.Open();
adapter = new SqlDataAdapter(Sql, connection);
adapter.Fill(ds);
connection.Close();
modify_gv.DataSource = ds.Tables[0];
} catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
private void modify_gv_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
try {
cmdBuilder = new SqlCommandBuilder(adapter);
changes = ds.GetChanges();
if(changes != null)
adapter.Update(changes);
// MessageBox.Show("Changes Done");
} catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
然而,每一個我做的datagridview
發生變化時,變量「變化」總是空。你知道爲什麼嗎?
之前你叫'AcceptChanges'地方?這將清除數據集上的更改跟蹤。 – 2012-07-12 12:11:59
我試過cmdBuilder = new SqlCommandBuilder(adapter); ds.AcceptChanges(); changes = ds.Tables [0] .GetChanges();但更改仍爲空 – 2012-07-12 13:08:52
不,我的意思是*不*調用'AcceptChanges'這是否會導致'GetChanges'不返回任何內容。 – 2012-07-12 13:18:03