简体   繁体   English

如何在datagridview中更新单元格?

[英]how to update cell in datagridview?

i have datagridview that connect to my database (access) 我有datagridview连接到我的数据库(访问)

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. 但是当我刷新时,我看到该值又回到了原始值。

how i can update this cell (without sql query) 我怎么能更新这个单元格(没有sql查询)

i bind dataset to datagridview like this: 我将数据集绑定到datagridview,如下所示:

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..... 拜托,我必须找到怎么做.....

thank's in advance 提前致谢

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

To update your database you will have to call BindingSource.EndEdit() and then TableAdapter.Update(). 要更新数据库,您必须调用BindingSource.EndEdit(),然后调用TableAdapter.Update()。 After you have done this, you can refresh your data. 完成此操作后,您可以刷新数据。

EDIT 编辑

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. 在我之前的回答中,我假设您使用设计器创建了所有内容,因为您不想使用SQL来进行更新。 Obviously i was wrong. 显然我错了。

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. 要实现您的目标,您应该使用OleDbCommandBuilder类,它给出一个SELECT命令,自动为您的表生成简单的编辑命令(插入/更新/删除)。

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");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM