如何获取 WPF DataGrid 将更改保存回数据库?

发布于 2024-07-29 02:11:28 字数 190 浏览 3 评论 0原文

如何让 WPF DataGrid 将更改保存回数据库?

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

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

有谁知道我错过了什么?

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬心 2024-08-05 02:11:28

执行更新

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

以下示例演示如何处理 RowChanged 和 RowDeleted 事件,以便每次用户更改一行时,DataTable 状态的更改都会写入数据库:

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);
}

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文