简体   繁体   中英

how to update cell in 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.

how i can update this cell (without sql query)

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.....

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. 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 ).

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.

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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