简体   繁体   English

像Access一样,如何使用ac#datagridview更新数据库文件?

[英]How to use a c# datagridview to update a database file just like Access does?

I have googled everywhere and I am finally giving up and asking here. 我到处都用Google搜索了,我终于放弃了,在这里问。

I am working in Visual Studio 2010 with C#. 我正在使用C#在Visual Studio 2010中工作。 I have set up a form with a datagridview connected to a MSSQL database and I need to save changes made in the datagridview back to the physical database. 我已经建立了一个带有连接到MSSQL数据库的datagridview的表单,并且需要将datagridview中所做的更改保存回物理数据库。 I am having some success but I think that I am going about some of it completely wrong because I can not get it to save consistently. 我取得了一些成功,但我认为我会完全犯错,因为我无法始终如一地节省下来。

What I really want is for the updates to work just like they do when working with Access. 我真正想要的是使更新能够像使用Access一样工作。 When I edit a row in the datagridview and then leave that row, either by selecting another row or by selecting some other control on the form or even by changing to another form or quitting the application the row should be automatically pdated to the physical database. 当我在datagridview中编辑一行然后离开该行时,可以通过选择另一行或通过选择表单上的某些其他控件,甚至通过更改为另一种表单或退出应用程序来退出该行,该行应自动添加到物理数据库中。

The first part of this question I think is, what is the proper event to use to trigger the save and then second what methods should be used to actually write the data to the database? 我认为这个问题的第一部分是,用于触发保存的适当事件是什么,其次,应该使用什么方法将数据实际写入数据库?

there are a lot of events that could be used including 有很多事件可以使用,包括

Leave Event which Occurs when the input focus leaves the DataGridView 当输入焦点离开DataGridView时发生的Leave事件

CellLeave Event which Occurs when a cell loses input focus and is no longer the current cell CellLeave事件,当一个单元格失去输入焦点并且不再是当前单元格时发生

RowLeave Event which Occurs when a row loses input focus and is no longer the current row 当行失去输入焦点并且不再是当前行时发生的RowLeave事件

MSDN has info on each of the DataGridView Events MSDN包含有关每个DataGridView Events

In the Event you use, you would place your code to perform the UPDATE . 在使用的Event ,您将放置代码以执行UPDATE There are lots of answers on SO about updating the database: 关于更新数据库,有很多答案:

WinForms DataGridView - update database WinForms DataGridView-更新数据库

C# WinForms - how to send updates from DataGridView to DataBase C#WinForms-如何将更新从DataGridView发送到数据库

My suggestion would be try try some of this code and then if you have problems, post another question with your code. 我的建议是尝试尝试一些此代码,然后如果遇到问题,请在代码中发布另一个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中使用datagridview更新Access数据库 - How to update Access database using a datagridview in C# 如何使用C#将datagridview更新到数据库? - how to update datagridview to database using c#? 如何在C#Windows应用程序中动态更新datagridview中的行,就像不使用数据库在Asp.net Web应用程序中添加到购物车一样 - How to update rows in datagridview dynamically in C# windows application just like Add to Cart in Asp.net Web application without using database 使用C#用CSV文件填充DataGridView,并使用结果更新Access数据库 - Using C# to populate a DataGridView with CSV file, and update Access database with results 如何在使用MS Access数据库的C#中使用Like运算符? - How to use Like operator in c# using ms access database? 如何在C#中使用datagridview列名更新Access数据库的特定列 - How to update specific columns of access database using datagridview column name in C# c# - 如何为datagridview实现更新按钮并使用dataHandler类(数据访问类)将值保存到数据库 - how to implement update button for datagridview and save values to database using a dataHandler class(data access class) c# 在C#中编辑datagridview后如何更新数据库 - How to update my database after editing the datagridview in c# 如何使用dataGridView C#中的行更新数据库中的行 - How to update rows in my database with rows from dataGridView C# 如何使用C#将DataGridView数据插入Access数据库表中? - How to insert DataGridView data in to table of access database using c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM