C# - DataTable.AcceptChanges() in a data table


C# - DataTable.AcceptChanges() in a data table

About DataTable.AcceptChanges Method (System.Data)

The DataTable’s AcceptChanges() method commits the changed done to the DataRow and the DataTable’s RejectChanges() method rolls back the changes done to the DataRow.

Example: Suppose I add a new row to a datatable and perform various operations on the row to observe the changes in RowState of the row.

            DataTable dtEmployee = new DataTable();
            DataRow drEmp;

            // Add a new Row and perform data changes
            drEmp = dtEmployee.NewRow();   // RowState - Detached
            dtEmployee.Rows.Add(drEmp);    // RowState - Added
            dtEmployee.AcceptChanges();    // RowState - Unchanged

            drEmp["ID"] = 2312;            // RowState - Modified
            dtEmployee.AcceptChanges();    // RowState - Unchanged

            drEmp.Delete();                // RowState - Deleted
            dtEmployee.AcceptChanges();    // RowState - Detached

            gvEmployees.DataSource = dtEmployee;

So when you call the DataTable’s AcceptChanges(), the changes are committed to the DataTable and the RowState is reset to Unchanged.

RowState before calling AcceptChanges()
RowState after calling AcceptChanges()
Impact on the Row
New row is commited to the DataTable
Data Changes to the row are committed to the DataTable
Row is Permanently Removed from the DataTable

Referenced by http://msdn.microsoft.com/en-IN/library/system.data.datatable.acceptchanges.aspx

C# - DataTable.AcceptChanges() in a data table Reviewed by Ravi Kumar on 10:51 PM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017

Contact Form


Email *

Message *

Powered by Blogger.