C# 将 Datagridview 值更新到数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10683570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating Datagridview values into database
提问by 62071072SP
I am developing an forms application where user can get tables data from database into gridviewcontrol which is editable so that user can update some data in the grid and when user clicks update buttonon the form the updated datagrid values should update in database. User can update more column values at a time. I am working on datagrids for the frist time and I managed to get data from databse and display in data grid but I am not able to update edited data back top database.
我正在开发一个表单应用程序,用户可以在其中将表数据从数据库获取到可编辑的 gridviewcontrol 中,以便用户可以更新网格中的某些数据,并且当用户单击表单上的更新按钮时,更新后的数据网格值应该在数据库中更新。用户可以一次更新更多列值。我第一次在数据网格上工作,我设法从数据库中获取数据并显示在数据网格中,但我无法更新已编辑的数据备份数据库。
采纳答案by Alex Kryvdyk
Check the links below.
检查下面的链接。
http://www.codeproject.com/Articles/12846/Auto-Saving-DataGridView-Rows-to-a-SQL-Server-Data
http://www.codeproject.com/Articles/12846/Auto-Saving-DataGridView-Rows-to-a-SQL-Server-Data
C# Issue: How do I save changes made in a DataGridView back to the DataTable used?
C# 问题:如何将 DataGridView 中所做的更改保存回使用的 DataTable?
WinForms DataGridView - update database
C# WinForms - how to send updates from DataGridView to DataBase
回答by isioutis
If you are using a DataTable as a source to the DataGrid that shouldn't be so hard , you can get the changes in the DataTable using datatable.getchanges() // search for an example in getchanges()
如果您使用 DataTable 作为 DataGrid 的源,应该不会那么难,您可以使用 datatable.getchanges() // 在 getchanges() 中搜索示例来获取 DataTable 中的更改
and then update each row that has changed using the unique key ( i assume you have one ) of the row .
然后使用该行的唯一键(我假设您有一个)更新已更改的每一行。
If you need to know how you update back to the database then you should check msdn for the exact method you have to use , depending of course on the one you used to load the data to the grid .
如果您需要知道如何更新回数据库,那么您应该检查 msdn 以了解您必须使用的确切方法,当然这取决于您用来将数据加载到网格的方法。

