DataRow.AcceptChanges 方法

定义

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

public:
 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 时,将打印其 RowStateAs 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);

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

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

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

    row.Delete();
    // 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"));
    table.Columns.Add(fnameColumn);
    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)
 
    table.Rows.Add(row)
    ' New row.
    Console.WriteLine("AddRow " & row.RowState)
 
    table.AcceptChanges()
    ' Unchanged row.
    Console.WriteLine("AcceptChanges " & row.RowState)
 
    row("FirstName") = "Scott"
    ' Modified row.
    Console.WriteLine("Modified " & row.RowState)
 
    row.Delete()
    ' 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"))
    table.Columns.Add(fnameColumn)
    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.

适用于

另请参阅