DataRow.AcceptChanges 方法

提交自上次调用 AcceptChanges 以来对该行进行的所有更改。

**命名空间:**System.Data
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public Sub AcceptChanges
用法
Dim instance As DataRow

instance.AcceptChanges
public void AcceptChanges ()
public:
void AcceptChanges ()
public void AcceptChanges ()
public function AcceptChanges ()

异常

异常类型 条件

RowNotInTableException

此行不属于该表。

备注

在调用 AcceptChanges 时,EndEdit 方法被隐式调用,以便终止任何编辑。如果行的 RowState 原来是“Added”或“Modified”,则 RowState 将变成“Unchanged”。如果 RowState 是“删除”,则该行将被移除。

有关更多信息,请参见 BeginEdit 方法。

DataTable 类还具有 AcceptChanges 方法,此方法会影响整个表中所做的更改。

示例

下面的示例首先创建有一个列的新 DataTable,然后创建单个 DataRow。当创建、添加、修改和删除 DataRow 时,将输出其 RowState

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
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;
}

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

DataRow 类
DataRow 成员
System.Data 命名空间
AcceptChanges
BeginEdit
CancelEdit
DataViewRowState
DataTable
EndEdit
HasVersion
Item
RowState