C# .NET - Save to Database (mdb) changes in DataGridView using Update() Method

Asked By Aldo Liaks on 23-Mar-09 04:23 AM
Guys, I am reading lots of articles, but I still can't find the error in my code (below). Is there any chance that you help me to find the problem? When running the form, the DGV shows the data, (3 rows, 3 cols). -. I change the value in row 1, col 2 => nothing happens. -. I change the value in row 1, col 2 and row 2, col 2 => Change in database only for row 1, col 2 -. I change the value in row 1, col 2 and row 2, col 2 and row 3, col 2 => Change in database only for row 1, col 2 and row 2, col 2 As you see, the last change never updates the database... Thanks in advance for any help, Aldo. public frmGrid() { InitializeComponent(); System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\\Visual Studio 2008\\Projects\\...Db1.mdb; Jet OLEDB:Database Password=x;"); System.Data.OleDb.OleDbCommand oleDbCommand = new System.Data.OleDb.OleDbCommand("SELECT * FROM Customers;"); oleDbCommand.Connection = conn; //DataSet, OleDbDataAdapter, OleDbCommandBuilder were declared as static. ds = new System.Data.DataSet(); daOleDb = new System.Data.OleDb.OleDbDataAdapter(oleDbCommand); cbOleDb = new System.Data.OleDb.OleDbCommandBuilder(daOleDb); daOleDb.Fill(ds, "t1"); dgv1.DataSource = ds; dgv1.DataMember = "t1"; dgv1.CellEndEdit += new DataGridViewCellEventHandler(dgv1_CellEndEdit); } private void dgv1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { daOleDb.Update(ds, "t1"); }
Vasanthakumar D replied to Aldo Liaks on 23-Mar-09 04:32 AM

Hi,

its common.. becuase after you change the data in last column you have to commit the change by leaving the current cell.... using enter key or tab key.

I do click Enter

Aldo Liaks replied to Vasanthakumar D on 23-Mar-09 04:52 AM
Hi man, I do click Enter after updating the cell, but nothing happens... what's missing?

pls post the code in readable form....

Vasanthakumar D replied to Aldo Liaks on 23-Mar-09 05:15 AM
end of post
once again
Aldo Liaks replied to Vasanthakumar D on 23-Mar-09 05:20 AM
Guys, I am reading lots of articles, but I still can't find the error in my code (below). When running the form, the DGV shows the data, (3 rows, 3 cols). -. I change the value in row 1, col 2 => nothing happens. -. I change the value in row 1, col 2 and row 2, col 2 => Change in database only for row 1, col 2 -. I change the value in row 1, col 2 and row 2, col 2 and row 3, col 2 => Change in database only for row 1, col 2 and row 2, col 2 As you see, the last change never updates the database... Evaluating the line ds.HasChanges() I see that for the first value, I always get "false". Thanks in advance for any help, Aldo. public frmGrid() { InitializeComponent(); System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\\Visual Studio 2008\\Projects\\...Db1.mdb; Jet OLEDB:Database Password=x;"); System.Data.OleDb.OleDbCommand oleDbCommand = new System.Data.OleDb.OleDbCommand("SELECT * FROM Customers;"); oleDbCommand.Connection = conn; //DataSet, OleDbDataAdapter, OleDbCommandBuilder were declared as static. ds = new System.Data.DataSet(); daOleDb = new System.Data.OleDb.OleDbDataAdapter(oleDbCommand); cbOleDb = new System.Data.OleDb.OleDbCommandBuilder(daOleDb); daOleDb.Fill(ds, "t1"); dgv1.DataSource = ds; dgv1.DataMember = "t1"; dgv1.CellEndEdit += new DataGridViewCellEventHandler(dgv1_CellEndEdit); } private void dgv1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { daOleDb.Update(ds, "t1"); }
...
Aldo Liaks replied to Aldo Liaks on 23-Mar-09 05:22 AM
public frmGrid(string menuName, ResourceManager[] rmArray, string querySource) { InitializeComponent(); System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\\Visual Studio 2008\\Projects\...\Db1.mdb; Jet OLEDB:Database Password=x;"); System.Data.OleDb.OleDbCommand oleDbCommand = new System.Data.OleDb.OleDbCommand("SELECT * FROM Customers;"); oleDbCommand.Connection = conn; ds = new System.Data.DataSet(); daOleDb = new System.Data.OleDb.OleDbDataAdapter(oleDbCommand); cbOleDb = new System.Data.OleDb.OleDbCommandBuilder(daOleDb); daOleDb.Fill(ds, "t1"); dgv1.DataSource = ds; dgv1.DataMember = "t1"; dgv1.CellEndEdit += new DataGridViewCellEventHandler(dgv1_CellEndEdit); } private void dgv1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { MessageBox.Show(ds.HasChanges().ToString()); daOleDb.Update(ds, "t1"); }
What's happening here?!
Aldo Liaks replied to Aldo Liaks on 23-Mar-09 05:25 AM
Guys, I am reading lots of articles, but I still can't find the error in my code (below). When running the form, the DGV shows the data, (3 rows, 3 cols). -. I change the value in row 1, col 2 => nothing happens. -. I change the value in row 1, col 2 and row 2, col 2 => Change in database only for row 1, col 2 -. I change the value in row 1, col 2 and row 2, col 2 and row 3, col 2 => Change in database only for row 1, col 2 and row 2, col 2 As you see, the last change never updates the database... Evaluating the line ds.HasChanges() I see that for the first value, I always get "false", and then I get "true". Thanks in advance for any help, Aldo. public frmGrid() { InitializeComponent(); System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\\Vi...Db1.mdb; Jet OLEDB:Database Password=x;"); System.Data.OleDb.OleDbCommand oleDbCommand = new System.Data.OleDb.OleDbCommand("SELECT * FROM Customers;"); oleDbCommand.Connection = conn; //DataSet, OleDbDataAdapter, OleDbCommandBuilder were declared as static. ds = new System.Data.DataSet(); daOleDb = new System.Data.OleDb.OleDbDataAdapter(oleDbCommand); cbOleDb = new System.Data.OleDb.OleDbCommandBuilder(daOleDb); daOleDb.Fill(ds, "t1"); dgv1.DataSource = ds; dgv1.DataMember = "t1"; dgv1.CellEndEdit += new DataGridViewCellEventHandler(dgv1_CellEndEdit); } private void dgv1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { daOleDb.Update(ds, "t1"); }
button event
Asked By Aldo Liaks on 23-Mar-09 06:56 AM
I tried moving the Update() method to the Click event of a button, and it DOES work... I tried to force the dgv to stop editing by dgv1.EndEdit(); daOleDb.Update(ds); but still doesn't update the last change in the DGV...
Vasanthakumar D replied to Aldo Liaks on 23-Mar-09 08:59 AM
Hi,
try this
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataSet ds = new DataSet();
            BindingSource bnd = (BindingSource)dataGridView1.DataSource;
            ds = (DataSet)bnd.DataSource;
            DataTable dt = ds.GetChanges();
            if (dt != null)
            {
                namesTableAdapter.Update(x);
                ds.AcceptChanges();
            }
        }
SOLVED
Aldo Liaks replied to Vasanthakumar D on 23-Mar-09 09:44 AM
Thanks man! private void dgv1_CellEnter(object sender, DataGridViewCellEventArgs e) { DataSet ds2 = new DataSet(); ds2 = (DataSet)dgv1.DataSource; DataTable dt = ds2.Tables[0].GetChanges(); if (dt != null) { int currCellRowNo = dgv1.CurrentCell.RowIndex; int currCellColNo = dgv1.CurrentCell.ColumnIndex; daOleDb.Update(dt); ds.AcceptChanges(); this.dgv1.CurrentCell = this.dgv1[currCellColNo, currCellRowNo]; } }
dummy ahuja replied to Aldo Liaks on 25-Oct-10 07:50 AM
Check this useful link:

http://www.mindstick.com/Articles/4c1f8967-0a8b-4788-a22e-d62db7095175/