C# .NET - DataGridView - OleDbDataAdapter with OleDbCommandBuilder and Update() Method

Asked By Aldo Liaks on 22-Mar-09 07:30 AM
Hi guys, I have a Windows form with a DataGridView. I want to populate data from MS Access (.mdb) to my DataGridView. (This is working). Now, I want to make changes to my data in the DGV and see those changes reflected in my Database table. This is working partially. If I make more than one change, the database gets the updated data, but if I make only one change, nothing happens in the database. The other problem I have is that if I make changes throw the row nothing happens. Only after changing data in a column (the rows of the same column) I get the changes reflected to the db. I use the following code: private void DataFromAccess() { System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connString + dataSource); System.Data.OleDb.OleDbCommand oleDbCommand = new System.Data.OleDb.OleDbCommand(sqlString); oleDbCommand.CommandType = CommandType.Text; oleDbCommand.Parameters.AddWithValue(parameterName, parameterValue); oleDbCommand.Connection = conn; tableName = "t1"; ds = new System.Data.DataSet(); daOleDb = new System.Data.OleDb.OleDbDataAdapter(oleDbCommand); cbOleDb = new System.Data.OleDb.OleDbCommandBuilder(daOleDb); daOleDb.Fill(ds, tableName); dgv.DataSource = ds; dgv.DataMember = tableName; oleDbCommand = null; conn = null; } I also created the following CellEvent: private void dgvGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e) { daOleDb.Update(ds, tableName); } Thanks in advance for any help. Aldo.

Still there... - Asked By Aldo Liaks on 22-Mar-09 08:21 AM

Hi, The problem is still there... :((

READ THIS - C_A P replied to Aldo Liaks on 23-Mar-09 05:39 AM

By the name of the table.

The easiest way to do this is to set the name of your datagridview to the name of the table.

Use you OleDbDataAdapter's Update method to do this. Just let it know the name of the table and the data to update.

You can google or search the fourm here on how to build statements with your DataAdapter's Update method. Shouldn't be to hard.

TRY THIS - C_A P replied to Aldo Liaks on 23-Mar-09 05:40 AM

you bind the values from the database (using the dataAdapter) to the datagridview and set the datagridview datasource to the dataset defaultview:


this.theDataGridView.DataSource = theDataSet.Tables[0].DefaultView;


then when you add rows to the datagridview, simply call the Update() command on the dataAdapter which will update the database automatically.


you could manually add a new row in the dataTable, and add columns of data then re-bind the dataGridView to the dataset, since it will have an updated set of records, then to finally commit the changes to the database, simply call the Update() command on the dataAdapter.


To add a row in the datatable:


theDataSet.Tables[0].Rows.Add(objectValues);


theObjectValues are a collection of objects which bind the values to the columns, so if we had 2 columns we would do this:


object[] theRecord = new object[] { "Column1Value", "Column2Value" };

theDataSet.Tables[0].Rows.Add(theRecord);

this.theDataGridView.DataSource = theDataSet.Tables[0].DefaultView;


//then commit to database:

theDataAdapter.Update();


READ THIS - C_A P replied to Aldo Liaks on 23-Mar-09 05:41 AM
YOU CAN UPDATE DATABASE THROUGH DATAGRIDVIEW. CONNECT TO YOUR DATABASE. ADD A DATAGRIDVIEW. USE FILL METHOD TO DISPLAY DATA.
CREATE A DATATABLE. CREATE A BINDING SOURCE. ATTACH THE DATA TO THE DATATABLE FROM DATAGRIDVIEW THROUGH BINDINGSOURCE.

THEN USE UPDATE METHOD OF DATAADAPTER TO UPDATE DATABASE.

VISIT THE FOLLOWING WEB SITE FOR DETAILS.


http://www.articlescloud.com/
TRY THIS - C_A P replied to Aldo Liaks on 23-Mar-09 05:42 AM

http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-database