DataRow.BeginEdit 方法

DataRow 对象开始编辑操作。

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

语法

声明
Public Sub BeginEdit
用法
Dim instance As DataRow

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

异常

异常类型 条件

InRowChangingEventException

RowChanging 事件中已调用该方法。

DeletedRowInaccessibleException

对已删除的行调用该方法。

备注

使用 BeginEdit 方法将 DataRow 置于编辑模式。在此模式中,事件被临时挂起,以便允许用户在不触发验证规则的情况下对多行进行多处更改。例如,如果需要确保总数列的值等于某行中借贷列的值,则可以将每一行都置入编辑模式,以便在用户尝试提交值之前挂起对行值的验证。

BeginEdit 方法在用户更改数据绑定控件的值时被隐式调用;EndEdit 方法在您调用 DataTable 对象的 AcceptChanges 方法时被隐式调用。当处于该编辑模式时,DataRow 存储原始值和新建议值的表示形式。因此,只要尚未调用 EndEdit 方法,就可以通过传递 Item 属性的 version 参数的 DataRowVersion.OriginalDataRowVersion.Proposed 来检索原始版本或建议版本。此时,还可通过调用 CancelEdit 方法取消任何编辑。

若要查看行中是否包含原始值或建议值,请调用 HasVersion 方法。

示例

该示例创建一个简单的 DataTable,该表有一个 DataColumn、五个 DataRow 对象和一个 UniqueConstraint。还添加了 RowChanged 事件处理程序以监视行的值何时发生更改。在针对现有行调用 BeginEdit 之后,约束和事件被临时禁用,原始值和建议值将打印出来。BeginEdit 被再次调用,以便为两行设置相同的值。当调用 EndEdit 时,将针对相同的值实施 UniqueConstraint

Private Sub DemonstrateRowBeginEdit()
    Dim table As New DataTable("table1")
    Dim column As New DataColumn("col1", Type.GetType("System.Int32"))
    AddHandler table.RowChanged, AddressOf Row_Changed
    table.Columns.Add(column)

    ' Add a UniqueConstraint to the table.
    table.Constraints.Add(New UniqueConstraint(column))

    ' Add five rows.
    Dim newRow As DataRow
       
    Dim i As Integer
    For i = 0 To 4
        ' RowChanged event will occur for every addition.
        newRow = table.NewRow()
        newRow(0) = i
        table.Rows.Add(newRow)
    Next i

    ' AcceptChanges.
    table.AcceptChanges()

    ' Invoke BeginEdit on each.
    Console.WriteLine(ControlChars.Cr _
       & " Begin Edit and print original and proposed values " _
       & ControlChars.Cr)
    Dim row As DataRow
    For Each row In  table.Rows           
        row.BeginEdit()
        row(0) = CInt(row(0)) & 10
        Console.Write(ControlChars.Tab & " Original " & ControlChars.Tab _
           & row(0, DataRowVersion.Original).ToString())
        Console.Write(ControlChars.Tab & " Proposed " & ControlChars.Tab _
           & row(0, DataRowVersion.Proposed).ToString() & ControlChars.Cr)
    Next row
    Console.WriteLine(ControlChars.Cr)

    ' Accept changes
    table.AcceptChanges()

    ' Change two rows to identical values after invoking BeginEdit.
    table.Rows(0).BeginEdit()
    table.Rows(1).BeginEdit()
    table.Rows(0)(0) = 100
    table.Rows(1)(0) = 100
    Try
        ' Now invoke EndEdit. This will cause the UniqueConstraint
        ' to be enforced.
        table.Rows(0).EndEdit()
        table.Rows(1).EndEdit()
    Catch e As Exception
    ' Process exception and return.
        Console.WriteLine("Exception of type {0} occurred.", _
           e.GetType().ToString())
    End Try
End Sub

Private Sub Row_Changed _
(sender As Object, e As System.Data.DataRowChangeEventArgs)
    Dim table As DataTable = CType(sender, DataTable)
    Console.WriteLine("RowChanged " & e.Action.ToString() _
       & ControlChars.Tab & e.Row.ItemArray(0).ToString())
End Sub
private void DemonstrateRowBeginEdit()
{
    DataTable table = new DataTable("table1");
    DataColumn column = new 
        DataColumn("col1",Type.GetType("System.Int32"));
    table.RowChanged+=new 
        DataRowChangeEventHandler(Row_Changed);
    table.Columns.Add(column);

    // Add a UniqueConstraint to the table.
    table.Constraints.Add(new UniqueConstraint(column));

    // Add five rows.
    DataRow newRow;
   
    for(int i = 0;i<5; i++)
    {
        // RowChanged event will occur for every addition.
        newRow= table.NewRow();
        newRow[0]= i;
        table.Rows.Add(newRow);
    }
    // AcceptChanges.
    table.AcceptChanges();

    // Invoke BeginEdit on each.
    Console.WriteLine(
        "\n Begin Edit and print original and proposed values \n");
    foreach(DataRow row in table.Rows)
    {
   
        row.BeginEdit();
        row[0]=(int) row[0]+10;
        Console.Write("\table Original \table" + 
            row[0, DataRowVersion.Original]);
        Console.Write("\table Proposed \table" + 
            row[0,DataRowVersion.Proposed] + "\n");
    }
    Console.WriteLine("\n");
    // Accept changes
    table.AcceptChanges();
    // Change two rows to identical values after invoking BeginEdit.
    table.Rows[0].BeginEdit();
    table.Rows[1].BeginEdit();
    table.Rows[0][0]= 100;
    table.Rows[1][0]=100;
    try
    {
        /* Now invoke EndEdit. This will cause the UniqueConstraint
           to be enforced.*/
        table.Rows[0].EndEdit();
        table.Rows[1].EndEdit();
    }
    catch(Exception e)
    {
        // Process exception and return.
        Console.WriteLine("Exception of type {0} occurred.", 
            e.GetType());
    }
}
 
private void Row_Changed(object sender, 
    System.Data.DataRowChangeEventArgs e)
{
    DataTable table = (DataTable)  sender;
    Console.WriteLine("RowChanged " + e.Action.ToString() 
        + "\table" + e.Row.ItemArray[0]);
}

平台

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
CancelEdit
EndEdit
HasVersion
Item
RowState