简体   繁体   中英

How to make WPF DataGrid save changes back to database?

I have search around and found some answers, but I think my case a bit different. Here is my XAML tag:

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

and here is the C# code:

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

The DataGrid show data ok. How do I save changes bac to database? I have try:

a.Update(d);

But there is a exception say:

Update unable to find TableMapping['Table'] or DataTable 'Table'.

Well, I should search harder be for post, anyway here is the solution:

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

For more detail: http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM