Question update changes from DataGridView Back to DataTable

pooya1072

Well-known member
Joined
Jul 5, 2012
Messages
84
Programming Experience
Beginner
hi
i have a datagridview in my form that it get Data from a datatable ( a dataadapter fill this datatable from a sql database )
i change some data in datagridview (for example add or delete some rows , change cells content or ...) and i want this change back to datatable and finally update the database.how can i send changes to datatable from datagridview ? what is the best way to do this ?
 
Hi,

Presumably, you have set the DataSource of the DataGridView to the DataTable in question? If so and, if this is a Strongly Typed Dataset created with the VS IDE, then you would just call the DataAdapters Update method passing the DataTable or Rows to be updated.

If this is NOT a Strongly typed DataSet or DataTable then you will need to use a CommandBuilder to generate the Action statements for your DataAdapter and then call the Update method on that DataAdapter.

Hope that helps.

Cheers,

Ian
 
thanks about your attention... i will go forward step by step :

1-i know how to update my database with dataAdapter ---> (DataAdapter1.Update(DataTable1))
2-there is a datagridview in my form
3-this Datagridview get data from a datatable (this dataTable get data from a sql database ----> DataAdapter1.fill(DataTable1)
4-i run program
5-DataAdapter get data from sql database and fill DataTable ... DataTable is the data source of DataGridView ... so data will display in datagridview .( every thing is ok and i have not any problem BUT)
6-suppose i change some data in datagridview ... how can i transfer this changes to datatable ? transfer changes from datagridview to datatable !!!
thank you very much :smug:
 
Hi,

I think you have missed the point?

If you know how to update your database using the Update method of the DataAdapter and you have Bound your DataTable to the DataGridView then calling Update on the DataAdapter of that DataTable will take all the information you have changed in the DataGridView and update that in your database.

What you have to realise is that when you change anything in the DataGridView it is automatically reflected in the underlying DataTable.

Does that make better sense?

Cheers,

Ian
 
Back
Top