0
for(int i = 0; i < m_DataTable.Rows.Count; i++)
{
    m_DataTable.Rows[i]["WORKER"] = "test";
    m_DataTable.Rows[i].AcceptChanges();
}

m_DataTable.AcceptChanges();

Is there any reason that I can not see that this code does not update my dataTable?

madth3
  • 7,275
  • 12
  • 50
  • 74
  • 1
    It seems OK to me... (perhaps Row.AcceptChanges is even useless since you call AcceptChanges on the whole DataTable at the end). How do you notice that the datatable is not updated ? Do you inspect it through debug-watch, or is it bound to a DataGridView ? – digEmAll Mar 14 '11 at 11:50
  • The code is fine. If you are using DataGridView (as digEmAll assumed) then you need to refresh it after Datatable was updated - Datagridview.Refresh(). – 26071986 Mar 14 '11 at 12:11

1 Answers1

3

Shouldn't you be creating a data adaptor and call Update to update the dataset changes to your database, before you call AcceptChanges?

Something like:

using (DataAdaptor adaptor = new DataAdaptor("SELECT * FROM table", connection)) {
    using (CommandBuilder builder = new CommandBuilder(adaptor)) {
        adaptor.Update(m_DataTable);
    }
}

m_DataTable.AcceptChanges();
Stephen Chung
  • 14,497
  • 1
  • 35
  • 48
  • Correct... DataTable by itself is for "in memory" content displayed to the user and not that committed to actual disk. The DataAdapter allows the synchronization from local in-memory table to the server which it is connected to. (intended for @mariusgherman) – DRapp Mar 14 '11 at 12:17
  • I wasn t trying to update the database itself, just thought that could be the cause. Anyway you helped solve the problem after all. ty – mariusgherman Mar 14 '11 at 15:18