This project is read-only.

automatically save datagrid to database on cell changed

Sep 20, 2010 at 6:29 PM

I have an implementation of sourcegrid where a datagrid displays info from an MSAccess database.   It has a "save" button on the form, implementing the committing of changes in the grid back to the database.   I am interested in automatically saving after any cell is changed.   I have attempted to implement the controller solution as in sample 26, incorporating a call to the save button code.   It properly traps and identifies the changes to the grid, but does not commit back to the database.  Any help much appreciated. If there is a simpler solution that would override onvaluechanged directly, I would appreciate info on that as well.


I will try to explain what is going on.

I use a database adapter and dataset, loaded from query text and connection strings passed to the dataset,, as follows:

  public partial class SourceGridForm : Form
        private string sConnectionString;
        private System.Data.DataSet dataset;
        OdbcDataAdapter adapter = new OdbcDataAdapter();
        DevAge.ComponentModel.BoundDataView bd;


dataset = new System.Data.DataSet();
 OdbcConnection conn = new OdbcConnection(sConnectionString);
 adapter.SelectCommand = new OdbcCommand(sQueryText, conn);

bd = new DevAge.ComponentModel.BoundDataView(dataset.Tables[0].DefaultView);

 dataGrid1.DataSource = bd;


I set up the controller as in the example, passing it the form:


     ValueChangedEvent valueChangedController = new ValueChangedEvent(this); // (this); // rmm 9/20/2010

and set this controller for all cells:

    dataGrid1.Controller.AddController(valueChangedController); // 9/20/2010 rmm add value changed controller to editable columns


the event is taken from sample 26.

  public class ValueChangedEvent : SourceGrid.Cells.Controllers.ControllerBase

            private SourceGridForm mFrm;
            public ValueChangedEvent(SourceGridForm frm)
            mFrm = frm;

            public override void OnValueChanged(SourceGrid.CellContext sender, EventArgs e)
                base.OnValueChanged(sender, e);

                string val = "Value of cell {0} is '{1}'";
                MessageBox.Show(sender.Grid, string.Format(val, sender.Position, sender.Value));  // THIS WORKS
                mFrm.btnSave_Click(sender, e); // THIS DOES NOT UPDATE


the implementing code for save button is as follows, it works when the user hits the button on screen, but not when done in code from the cell value change

  private void btnSave_Click(object sender, EventArgs e)
            OdbcCommandBuilder cb = new OdbcCommandBuilder(adapter);
            adapter.InsertCommand = cb.GetInsertCommand();
            adapter.UpdateCommand = cb.GetUpdateCommand();


one other question, please.   sample 26 allow does not trigger the value change event unless you actually change the value in the cell (i.e. just double-clicking to  highlight it does not give you the message).   My implementation triggers the message on double-clicking the cell, even if I do not edit.  Can you tell me how to replicate the behavior of sample26?

Thanks in advance.

Dick Males,

Cincinnati, OH