0

I have a DataGridView which is binded to an Acces Database. I can update the database well if I make edits directly in the DataGridView, but if I use something like:

dgView.Rows[0].Cells[1].Value = "Some text";

It will write the text in the 1st cell at row 0 but then, when I call:

dAdapter.Update(dTable);

It won't update the database. Instead, the old value is preserved. If I would have edited the code myself and would have written "Some text" by double clicking the cell and typing in it, it would update fine. I know it is a problem ,but in fact I don't know what it is.

Please help, i really need to get this thing done (it is a project for school). Please ask me any further questions you need to help me. Thanks in advance.

Vali

Valentin Radu
  • 629
  • 1
  • 11
  • 26

1 Answers1

0

The problem here is that when you update your DataGridView programmatically the way you're doing it, the DataGridView's underlying data source (your DataTable) isn't being notified that it's been changed - so just the UI changes, not your data source.

You can use the DataGridView.BeginEdit method to fire these events like so:

dgView.BeginEdit(false);
dgView.Rows[0].Cells[1].Value = "Some text"; 
dgView.EndEdit();

Rather than attempting to update your DataTable programmatically through the DataGridView as you are, a better approach would be to update your DataTable directly.

Jay Riggs
  • 53,046
  • 9
  • 139
  • 151