如何获得WPF数据网格以将更改保存回数据库?

时间:2022-04-16 07:26:44

How do I get a WPF DataGrid to save changes back to the database?

如何获得WPF数据网格以将更改保存回数据库?

I've data-bound my DataGrid control to a DataTable object, and populated that table with a very simple SELECT query that retrieves some basic information. The data shows up just fine in the control.

我已经将DataGrid控件数据绑定到一个DataTable对象,并使用一个非常简单的SELECT查询填充该表,该查询检索一些基本信息。数据在控制中显示得很好。

But when I use the control to edit the data, the changes are not pushed back to the DB.

但是,当我使用控件来编辑数据时,这些更改不会被推回数据库。

Does anyone know what I'm missing?

有人知道我错过了什么吗?

1 个解决方案

#1


16  

Performing Updates

执行更新

When the user edits the Customers data within the DataGrid, the bound in-memory DataTable is updated accordingly. However, these updates are not automatically written back to the database. It is up to the developer to decide when changes to the DataTable are written back to the database depending on the requirements of the application. For example, in some cases, you might wish to submit a batch of changes via a "Submit" button, or you may wish to have the database updated as the user commits each row edit. In order to support these, the rows that the DataTable contains have a RowState property which indicates whether they contain changes which should be synchronized with the database. The synchronization process is easily achieved via the TableAdapter's Update method. url: WPF DataGrid examples

当用户在DataGrid中编辑客户数据时,绑定的内存中的数据表将相应地更新。但是,这些更新不会自动写回数据库。由开发人员决定何时根据应用程序的需求将DataTable的更改写回数据库。例如,在某些情况下,您可能希望通过“提交”按钮提交一批更改,或者希望在用户提交每一行编辑时更新数据库。为了支持这些,DataTable包含的行具有RowState属性,该属性指示它们是否包含应该与数据库同步的更改。通过TableAdapter的更新方法很容易实现同步过程。url:WPF DataGrid的例子

The following example shows how the RowChanged and RowDeleted events can be handled so that changes in the DataTable state are written to the database each time the user changes a row:

下面的示例展示了如何处理行更改和行删除事件,以便在用户每次更改一行时将数据表状态中的更改写入数据库:

public CustomerDataProvider()
{
    NorthwindDataSet dataset = new NorthwindDataSet();

    adapter = new CustomersTableAdapter();
    adapter.Fill(dataset.Customers);

    dataset.Customers.CustomersRowChanged +=
        new NorthwindDataSet.CustomersRowChangeEventHandler(CustomersRowModified);
    dataset.Customers.CustomersRowDeleted +=
        new NorthwindDataSet.CustomersRowChangeEventHandler(CustomersRowModified);
}

void CustomersRowModified(object sender, NorthwindDataSet.CustomersRowChangeEvent e)
{
    adapter.Update(dataset.Customers);
}

#1


16  

Performing Updates

执行更新

When the user edits the Customers data within the DataGrid, the bound in-memory DataTable is updated accordingly. However, these updates are not automatically written back to the database. It is up to the developer to decide when changes to the DataTable are written back to the database depending on the requirements of the application. For example, in some cases, you might wish to submit a batch of changes via a "Submit" button, or you may wish to have the database updated as the user commits each row edit. In order to support these, the rows that the DataTable contains have a RowState property which indicates whether they contain changes which should be synchronized with the database. The synchronization process is easily achieved via the TableAdapter's Update method. url: WPF DataGrid examples

当用户在DataGrid中编辑客户数据时,绑定的内存中的数据表将相应地更新。但是,这些更新不会自动写回数据库。由开发人员决定何时根据应用程序的需求将DataTable的更改写回数据库。例如,在某些情况下,您可能希望通过“提交”按钮提交一批更改,或者希望在用户提交每一行编辑时更新数据库。为了支持这些,DataTable包含的行具有RowState属性,该属性指示它们是否包含应该与数据库同步的更改。通过TableAdapter的更新方法很容易实现同步过程。url:WPF DataGrid的例子

The following example shows how the RowChanged and RowDeleted events can be handled so that changes in the DataTable state are written to the database each time the user changes a row:

下面的示例展示了如何处理行更改和行删除事件,以便在用户每次更改一行时将数据表状态中的更改写入数据库:

public CustomerDataProvider()
{
    NorthwindDataSet dataset = new NorthwindDataSet();

    adapter = new CustomersTableAdapter();
    adapter.Fill(dataset.Customers);

    dataset.Customers.CustomersRowChanged +=
        new NorthwindDataSet.CustomersRowChangeEventHandler(CustomersRowModified);
    dataset.Customers.CustomersRowDeleted +=
        new NorthwindDataSet.CustomersRowChangeEventHandler(CustomersRowModified);
}

void CustomersRowModified(object sender, NorthwindDataSet.CustomersRowChangeEvent e)
{
    adapter.Update(dataset.Customers);
}