共用方式為


DataRow.AcceptChanges 方法

定義

認可自前一次呼叫 AcceptChanges() 以來,對這個資料列所做的所有變更。

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

例外狀況

資料列不屬於資料表。

範例

下列範例會先使用一個資料列建立新的 DataTable ,然後建立單 DataRow一 。 建立、新增、修改和刪除時 DataRow ,會列印其 RowState

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 隱含呼叫 方法來結束任何編輯。 RowState如果數據列的 已加入或修改,則 RowState 會變成 [未變更]。 RowState如果 已刪除 ,則會移除資料列。

如需詳細資訊,請參閱 BeginEdit 方法。

類別 DataTable 也有一個 AcceptChanges 方法,會影響對整個數據表所做的變更。 如需詳細資訊和示範如何接受和拒絕個別數據列變更的程式代碼範例,請參閱 AcceptChanges 和 RejectChanges

適用於

另請參閱