簡體   English   中英

如何使WPF DataGrid將更改保存回數據庫?

[英]How to make WPF DataGrid save changes back to database?

我到處搜索並找到了一些答案,但是我認為我的情況有所不同。 這是我的XAML標簽:

<DataGrid Name="dataGrid1" 
          ItemsSource="{Binding Path=myPath}" 
          AutoGenerateColumns="True" />

這是C#代碼:

    a = new SqlDataAdapter("SELECT * FROM Patients", c);
    d = new DataSet();
    a.Fill(d, "myPath");
    dataGrid1.DataContext = d;

DataGrid顯示數據正常。 如何將更改bac保存到數據庫? 我嘗試了:

a.Update(d);

但是有一個例外說:

更新無法找到TableMapping ['Table']或DataTable'Table'。

好吧,我應該更努力地尋找帖子,無論如何,這是解決方案:

    SqlCommandBuilder builder = new SqlCommandBuilder(a);
    a.UpdateCommand = builder.GetUpdateCommand();
    a.Update(d);

有關更多詳細信息: http : //msdn.microsoft.com/zh-cn/library/system.data.common.dataadapter.update.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM