如何获取 WPF DataGrid 将更改保存回数据库?
如何让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
执行更新
当用户在 DataGrid 中编辑 Customers 数据时,绑定的内存中 DataTable 也会相应更新。 但是,这些更新不会自动写回数据库。 由开发人员根据应用程序的要求决定何时将数据表的更改写回数据库。 例如,在某些情况下,您可能希望通过“提交”按钮提交一批更改,或者您可能希望在用户提交每行编辑时更新数据库。 为了支持这些,DataTable 包含的行具有 RowState 属性,该属性指示它们是否包含应与数据库同步的更改。 同步过程可以通过TableAdapter 的Update 方法轻松实现。
网址:
WPF DataGrid 示例
以下示例演示如何处理 RowChanged 和 RowDeleted 事件,以便每次用户更改一行时,DataTable 状态的更改都会写入数据库:
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: