DataRow.BeginEdit 方法

定义

DataRow 对象启动编辑操作。

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

例外

已在 RowChanging 事件中调用该方法。

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

示例

该示例创建一个具有一DataColumn个和五DataRow个 对象的简单 DataTable ,以及一个 UniqueConstraintRowChanged还会添加事件处理程序,用于监视行的值何时发生更改。 对现有行调用 BeginEdit 后,将暂时禁用约束和事件,并打印原始值和建议的值。 BeginEdit再次调用 以将两行设置为同一值。 调用 时 EndEditUniqueConstraint 对相同的值强制实施 。

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

注解

BeginEdit使用 方法将 置于DataRow编辑模式。 在此模式下,事件会暂时挂起,允许用户对多行进行更改,而无需触发验证规则。 例如,如果必须确保总金额的列值等于行中的借方列和额度列的值,则可以将每行置于编辑模式,以暂停行值的验证,直到用户尝试提交值。

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

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

备注

方法 BeginEdit 暂时挂起 RowChanging 事件,但 delete 操作不会。

适用于

另请参阅