tags:

views:

41

answers:

1

hi

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

+2  A: 

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");
il_guru
i try this: dataGridView2.EndEdit(); dataGridView2.Update(); but it dont work
Gold
No, that's wrong. You have to use the bound object. Somewhere in your code there is the assigment dataGridView.DataSource = somethingBindingSource (probably in the form.Designer.cs file) and that is your BindingSource (the one to call EndEdit() on). You will also find a somethingTableAdapter to call the Update. How did you perform the binding between the datagrid and you Db?
il_guru
i still dont Understand, i update my question - how i work with database
Gold
Edited with a better answer
il_guru
thank's for the help, now i got this error: Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.
Gold
Did you set a primary key in your table?
il_guru