如何使用DataGridView更新数据库记录 [英] How to update database records using DataGridView

查看:66
本文介绍了如何使用DataGridView更新数据库记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我的朋友们......

我有一个Access数据库,包括表..那个表有一个是/否字段它的名字:IsHere,意思是真/假在C#中。另一方面,我使用Visual C#

创建了一个包含DataGridView的窗体...在这个窗体的加载事件中,我将从我的访问数据库填充这个DataGridView ..现在我想要离开点击IsHere的datagridview中的复选框来检查或取消选中此项,然后在此单击后更新数据库,使用DataGridView在数据库中的True / False值之间进行更改..

我希望我能清楚地解释我的问题,如果没有,我很抱歉,但我的英语不太好...

非常感谢你的朋友< 3.

解决方案

< blockquote>你可以展示你的代码,而不是解释它,你知道。

因为你不需要做一些假设。



希望你有一个 DataSet [ ^ ]或 DataTable ,作为您的数据层,然后使用 DataAdapter [ ^ ]作为数据库与 DataSet 之间的桥梁。另请参见从DataAdapter填充数据集 [ ^ ]



然后你应该使用 BindingSource [ ^ ]。 datagridview(v = vs.110).aspx> DataGridView [ ^ ]。这样可以更轻松地过滤数据并将数据成员连接到Windows窗体组件。



DataGridView 是您可以使用该事件的更改 DataGridView.OnCellValueChanged [ ^ ]。



在活动中你现在可以检查 DataRow 是新的或已更改,您可以采取适当的操作来插入或更新数据。在这里你也可以使用 DataAdapter ,参见使用DataAdapters更新数据源 [ ^ ]



所以,这个这是一个很长的答案,但是由于你没有展示你所做的事情,没有别的办法。


所以写一个复选框的更改事件并在里面连接到数据库并执行查询这将更新是/否列值。


Hello my friends...
I have an Access database including table .. that table have an "Yes/No" field "it's name: IsHere" that is meaning "True/False" in C#. on the other hand I created a windows form using Visual C#
including DataGridView... In "load" event for this Form I'll fill this DataGridView from my access database .. now I want to left click on checkbox in datagridview at "IsHere" to check or uncheck this then update database after this click to change between "True / False" value in database using DataGridView..
I hope I explained my problem clearly, If not, I am sorry but my English not good very well...
thank you so much my friends <3 .

解决方案

You could show your code instead of explaining it, you know.
As you don't I have to make some assumptions.

Hopefully you have a DataSet[^], or DataTable, as your data layer and then you use a DataAdapter[^] as a bridge between your database and the DataSet. See also Populating a DataSet from a DataAdapter[^]

Then you should use a BindingSource[^] between the DataSet and your DataGridView[^]. This makes it easier to filter the data and connect the data members to your Windows Form components.

To update or insert data when a value in the DataGridView is change you can use the event DataGridView.OnCellValueChanged[^].

Inside the event you can now check if the DataRow is new or changed and you can take the proper action to either insert or update the data. Here you can also use the DataAdapter, see Updating Data Sources with DataAdapters[^]

So, this was a long answer, but as you are not showing what you have done there is no other way.


So write the change event of checkbox and inside that connect to the database and execute a query which will update the yes/no column value.


这篇关于如何使用DataGridView更新数据库记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆