如何更新datagridview中的单元格? [英] how to update cell in datagridview?

查看:55
本文介绍了如何更新datagridview中的单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据网格视图连接到我的数据库(访问)

i have datagridview that connect to my database (access)

如果我停留在任何单元格上并更改值,则会看到值已更改

if i stay on any cell and change the value, a see that the value is changed

但是当我刷新时,我看到该值恢复到原始值.

but when i make refresh i see that the value is back to the original value.

我如何更新这个单元格(没有 sql 查询)

how i can update this cell (without sql query)

我像这样将数据集绑定到 datagridview:

i bind dataset to datagridview like this:

dsView = new DataSet();
            adp = new OleDbDataAdapter("SELECT * FROM Items", Conn);
            adp.Fill(dsView, "Items");
            this.dataGridView1.DataSource = dsView.Tables["Items"].DefaultView;
            adp.Dispose();

拜托,我必须找到方法.....

please, i must find how to do it.....

提前致谢

推荐答案

如果你的 datagridview 连接到数据库并且你不想使用 SQL,我可以假设它绑定到一个数据源.如果您通过 Visual Studio 设计器执行此操作,则应该有一个 TableAdapter(如果自动生成,可能称为 YOURTABLENAMETableAdapter)和一个 BindingSource(如果自动生成生成,可能称为 YOURTABLENAMEBidingSource).

If your datagridview is connected to the database and you do not want to use SQL, i can suppose it is bound to a datasource. If you performed that through the Visual Studio Designer there should be a TableAdapter (if automatically generated, probably called YOURTABLENAMETableAdapter) and a BindingSource (if automatically generated, probably called YOURTABLENAMEBidingSource).

要更新您的数据库,您必须先调用 BindingSource.EndEdit(),然后再调用 TableAdapter.Update().完成此操作后,您可以刷新数据.

To update your database you will have to call BindingSource.EndEdit() and then TableAdapter.Update(). After you have done this, you can refresh your data.

编辑

既然你提供了更好的信息,我可以给你一个更好的答案.在我之前的回答中,我假设您使用设计器创建了所有内容,因为您不想使用 SQL 进行更新.显然我错了.

Now that you have provided better information i can give you a better answer. In my previous answer, I assumed that you created everything with the designer because you didn't want to use SQL to make the update. Obviously i was wrong.

要实现您的目标,您应该使用 OleDbCommandBuilder 给定 SELECT 命令的类,它会自动为您的表生成简单的编辑命令(插入/更新/删除).

To achieve what you are looking for, you should use the OleDbCommandBuilder Class which, given a SELECT command, automatically generates simple editing command (insert/update/delete) for your table.

这是使用您的代码的示例:

here is an example using your code:

dsView = new DataSet();
adp = new OleDbDataAdapter("SELECT * FROM Items", Conn);
OleDbCommandBuilder cb = new OleDbCommandBuilder(adp);
adp.Fill(dsView, "Items");
this.dataGridView1.DataSource = dsView.Tables["Items"].DefaultView;
//adp.Dispose(); You should dispose you adp only when it is not loger needed (eg: after performing the update)

现在,在您对数据进行更改后,您可以调用

Now, after you perfomed your changes to the data you can call

adp.Update(dsView, "Items");

这篇关于如何更新datagridview中的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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