C# 中 DataSet 和 DataGridView 之间的数据绑定
我目前在表单上有一个 DataGridView,我想将其与 DataSet 中的 DataTable 一起使用,从 SQlite 数据库填充(使用 System.Data.SQlite)。
因此,我在数据库和DataSet之间有一个DataAdapter,可以将DataGridView数据源直接设置为DataTable。这显示得很好。
我的问题是这样的: 为什么我想在这里使用绑定源?很多教程都说可以用也可以不用。但除了增加一个额外的步骤之外,它还有什么用处呢?
另外,如果我希望在 DataGridView 更改时更新数据库,该怎么办? DataSet 是否自动更新 - 所以我只需要告诉 DataAdapter 更新?或者是否有绑定源有用的地方?
谢谢!
I currently have a DataGridView on a form which I want to use with a DataTable in a DataSet, populated from a SQlite database (using System.Data.SQlite).
So, I have a DataAdapter between the database and DataSet and can set the DataGridView data source directly as the DataTable. This displays fine.
My question is this:
Why would I want to use a Binding Source here? Many tutorials have said you can use it or not. But is there any use for it, other than adding an extra step?
Also, if I want the database to be updated when the DataGridView is changed, how can this be done? Is the DataSet automatically updated - so I just need to tell the DataAdapter to update? Or is there where a binding source is useful?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AFAIK,您必须在
DataSet
上调用GetChanges
,然后插入/更新/删除记录。AFAIK, you would have to call
GetChanges
on theDataSet
, and then insert/update/delete the records.对于其他搜索此内容的人,我找到了关于为什么使用绑定源的很好的描述 在这里。
同样,这解释了在 DataGridView 中所做的保存/恢复更改。
希望对某人有帮助!
For anyone else searching for this, I found a good description of why to use a Binding Source here.
Similarly, this explains saving/restoring changes made in the DataGridView.
Hope that helps someone!