2009-12-09 255 views
0

我的應用程序要求將WPF DataGrid控件中所做的更改保存回DataTable。我已經成功地將DataGrid中的數據保存到DataTable,但從DataGrid保存的數據未顯示我所做的更改,它僅顯示DataGrid首次填充時已存在的數據。WPF將DataGrid中的更改保存到數據表中

我有這麼遠:

public void UpdateQueueData(object sender, DataGridRowEditEndingEventArgs e) 
{ 
    if (e.EditAction == DataGridEditAction.Commit) 
    { 
     DataGridRow dgRow = e.Row; 
     DataRowView rowView = dgRow.Item as DataRowView; 
     DataRow drItem = rowView.Row; 
     Queue.Rows.RemoveAt(e.Row.GetIndex()); 
     Queue.ImportRow(drItem); 
     WriteXML(); 
    } 
} 

這工作,但它不保存更改,它只是保存DataRow,因爲它是它在DataGrid改變之前。

我錯過了什麼嗎?

回答

1

終於找到答案了!我得在

private void dataGrid_CurrentCellChanged(object sender, EventArgs e) 
    { 
     DataTable dt = ((DataView)dataGridQueue.ItemsSource).ToTable(); 
     /*Set the value of my datatable to the the changed version before 
     * writing it so a file   
     */ 
     dt.WriteXMLScema(... 
    } 
0

是否在您做出更改後致電AcceptChanges

+0

遺憾的是變更後的DataRow的,我的意思是在DataGrid中所做的更改均沒有出現在DataRow中:drItem時正在觸發事件。 – 2009-12-10 00:55:28

相關問題