Reputation: 1553
I'm trying to update a database after the changes in a DataGridView.
After changing the code several times, I've come to this one-liner:
primeTableAdapter.Update((PrimeDataSet.PrimeDataTable)(primeDataSet.Tables["Prime"]));
The method returns the correct number of rows added / edited and entering the form again without closing the entire application everything seems fine. The new rows are kept in the DataGridView. However, the Access file is not changed one bit, so when I relaunch the application they disappear.
I've bound the DataGridView to the table using the "Add New Data Source" wizard and I'm using Visual Studio 2010.
I've also found the code at: Inserting data from a DataGridView to a database and it seems similar to what I have to do, but I wasn't able to translate the vb.net code to something that would compile. However, I'm fairly sure it is really, really close to what I have to do.
Later Edit: I had added the database to the project. By setting the Copy to Output Directory property to Copy if Newer the changes were persistent between sessions.
Upvotes: 0
Views: 4448
Reputation: 784
if you want to pay,you can use a third party (Spire dataExport ) it make things more easier : to save datagrid into msAccess :
private void btnExportToAccess_Click(object sender, EventArgs e)
{
Spire.DataExport.Access.AccessExport accessExport = new
Spire.DataExport.Access.AccessExport();
accessExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
accessExport.DataTable = this.dataGridView1.DataSource as DataTable;
accessExport.DatabaseName = @"..\..\ToMdb.mdb";
accessExport.TableName = "ExportFromDatatable";
accessExport.SaveToFile();
}
and Here a link where you can find more clarification
Upvotes: 1