简体   繁体   中英

Save Changes to SQL Server Database from WPF Datagrid

I am using a WPF datagrid to display my contents in a SQL Server Database. The contents in the datagrid are binded to a view in the database. I populate the datagrid on window loaded by the following:

this.myAccountantDBDataSet = MyAccountant
    .MyAccountantDBDataSet(this.FindResource("myAccountantDBDataSet");

// Load data into the table AccountData.
this.accountDataAdapter = new MyAccountant
    .MyAccountantDBDataSetTableAdapters.AccountDataTableAdapter();
this.accountDataAdapter.Fill(myAccountantDBDataSet.AccountData);

System.Windows.Data.CollectionViewSource accountDataViewSource =
    (System.Windows.Data.CollectionViewSource)
    this.FindResource("accountDataViewSource");
accountDataViewSource.View.MoveCurrentToFirst();

What I am wondering, is how to save updates, inserts, and deletions back to the database since what I am showing in the datagrid is a view. The database table and views looks like this:

在此处输入图片说明

Updates will work, because the fields in the view map 1:1 to fields in the underlying tables, so Sql Server will be able to resolve updates in the view back to the tables (even AccountType ).

Inserts will not work because Account.AccountTypeId can not be set and I assume that it is a mandatory field.

Deletes will not work, because Sql Server cannot tell which record in which table you are trying to delete.

I think in this case, you can just as well work with Accounts records and show AccountTypeId in a DataGridComboBoxColumn that has AccountData as datasource, displays AccountData.AccountType and binds to Account.AccountTypeId .

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