DataRow.AcceptChanges 方法


提交自上次调用 AcceptChanges() 以来对该行进行的所有更改。Commits all the changes made to this row since the last time AcceptChanges() was called.

 void AcceptChanges();
public void AcceptChanges ();
member this.AcceptChanges : unit -> unit
Public Sub AcceptChanges ()


此行不属于该表。The row does not belong to the table.


下面的示例首先创建一个新 DataTable 的,其中包含一个列,然后创建一个 DataRowThe following example first creates a new DataTable with one column, and then creates a single DataRow. DataRow 创建、添加、修改和删除时, RowState 将打印其。As the DataRow is created, added, modified, and deleted, its RowState is printed.

private void DemonstrateAcceptChanges()
    //Run a function to create a DataTable with one column.
    DataTable table = MakeTable();
    DataRow row;

    // Create a new DataRow.
    row = table.NewRow();
    // Detached row.
    Console.WriteLine("New Row " + row.RowState);

    // New row.
    Console.WriteLine("AddRow " + row.RowState);

    // Unchanged row.
    Console.WriteLine("AcceptChanges " + row.RowState);

    row["FirstName"] = "Scott";
    // Modified row.
    Console.WriteLine("Modified " + row.RowState);

    // Deleted row.
    Console.WriteLine("Deleted " + row.RowState);

private DataTable MakeTable()
    // Make a simple table with one column.
    DataTable table = new DataTable("table");
    DataColumn fnameColumn = new DataColumn(
        "FirstName", Type.GetType("System.String"));
    return table;
Private Sub DemonstrateAcceptChanges()
    ' Run a function to create a DataTable with one column.
    Dim table As DataTable = MakeTable()
    Dim row As DataRow 
    ' Create a new DataRow.
    row = table.NewRow()
    ' Detached row.
    Console.WriteLine("New Row " & row.RowState)
    ' New row.
    Console.WriteLine("AddRow " & row.RowState)
    ' Unchanged row.
    Console.WriteLine("AcceptChanges " & row.RowState)
    row("FirstName") = "Scott"
    ' Modified row.
    Console.WriteLine("Modified " & row.RowState)
    ' Deleted row.
    Console.WriteLine("Deleted " & row.RowState)
 End Sub
 Private Function MakeTable()As DataTable
    ' Make a simple table with one column.
    Dim table As New DataTable("table")
    Dim fnameColumn As New DataColumn( _
        "FirstName", Type.GetType("System.String"))
    MakeTable = table
 End Function


调用时 AcceptChanges ,将 EndEdit 隐式调用方法以结束任何编辑。When invoking AcceptChanges, the EndEdit method is implicitly called to end any edits. 如果 RowState 添加或修改了行的,则将 RowState 保持不变。If the RowState of the row was Added or Modified, the RowState becomes Unchanged. 如果已 RowState 删除,则删除该行。If the RowState was Deleted, the row is removed.

有关更多信息,请参阅 BeginEdit 方法。See the BeginEdit method for more information.

DataTable类还具有一个 AcceptChanges 方法,该方法会影响对整个表所做的更改。The DataTable class also has an AcceptChanges method which affects changes made to the whole table. 有关演示如何接受和拒绝对单个数据行所做的更改的代码示例,请参阅 AcceptChanges 和 RejectChangesFor more information and a code example that demonstrates how to accept and reject changes to individual data rows, see AcceptChanges and RejectChanges.