DataRow.AcceptChanges DataRow.AcceptChanges DataRow.AcceptChanges DataRow.AcceptChanges Method

定义

提交自上次调用 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是创建、 添加、 修改和删除,其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);

    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 DataTable = New DataTable("table")
    Dim fnameColumn As DataColumn = 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的行是 Added 或 Modified,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.

适用于

另请参阅