Auto Saving DataGridView Rows back to Access Database

Hi,

I've been trying to auto save the DataGridView rows of my app back to the access database automatically but it doesnt work:

private DataRow _lastDataRow = null;

private void UpdateDatabase()
{
if (_lastDataRow != null && _lastDataRow.RowState == DataRowState.Modified)
historyTableAdapter.Update(_lastDataRow);
}

private void AccessDataSource_PositionChanged(object sender, EventArgs e)
{
BindingSource bindingSource = (BindingSource)sender;

DataRow thisDataRow = ((DataRowView)bindingSource.Current).Row;

if (thisDataRow == _lastDataRow) {
throw new ApplicationException("It seems the" +
" PositionChanged event was fired twice for" +
" the same row");
}

UpdateDatabase();

// track the current row for next
// PositionChanged event
_lastDataRow = thisDataRow;

}

Where am I going wrong Is there a simpler way to do this



Answer this question

Auto Saving DataGridView Rows back to Access Database

  • susiekay

    does _lastRow actually have a value I see it declared as a private global scope and the default value to null, does that value get changed at all Step through the debugger and step into the code line by line to see what the values are of objects just before the if statement and the UpdateDatabase() method

     

    the usualy way is really just to do:

    theDataAdapter.Update(theDataSet);

    or

    theDataAdapter.Update(theDataTable);

     



  • shmulik_segal

    No there are no errors or exceptions. Just that nothing is updated back to the database. Please can you point to the portions of the code that need to be rechecked.

    Thanks


  • sontek

    what doesnt work What happens Any errors What are they

    You should also avoid doing an update to the database when the user navigates to a different row for example, it will be expensive since databases are expensive to work with...can cause delays in your application, people would think your app is not responding etc....

    you would need to probably give the Update() command a datatable or a dataset - re looking at the code, its not going to work anyway since the DataRow is nothing (null) and you are updating the table adapter (DataAdapter) giving it nothing, no data to work with I believe. Could be wrong in this case however



  • Auto Saving DataGridView Rows back to Access Database