DataRow.BeginEdit DataRow.BeginEdit DataRow.BeginEdit DataRow.BeginEdit Method

定义

DataRow 对象启动编辑操作。Starts an edit operation on a DataRow object.

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

异常

对已删除的行调用该方法。The method was called upon a deleted row.

示例

该示例将创建一个简单DataTable与一个DataColumn和五个DataRow对象,和一个UniqueConstraintThe example creates a simple DataTable with one DataColumn and five DataRow objects, and a UniqueConstraint. 一个RowChanged还添加事件处理程序可以监视何时更改行的值。A RowChanged event handler is also added to monitor when the row's value is changing. 之后调用BeginEdit上的现有行,该约束和事件暂时禁用并打印原始和建议值。After invoking BeginEdit on the existing rows, the constraint and event are temporarily disabled and the original and proposed values are printed. BeginEdit再次调用将两行设置为相同值。The BeginEdit is again invoked to set two rows to the same value. EndEdit调用时,UniqueConstraint上完全相同的值强制执行。When EndEdit is called, the UniqueConstraint is enforced on the identical values.

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进入编辑模式。Use the BeginEdit method to put a DataRow into edit mode. 在此模式下,事件会暂停,以便允许用户在对多个行进行更改,而不会触发验证规则。In this mode, events are temporarily suspended, letting the user make changes to more than one row without triggering validation rules. 例如,如果您必须确保为总金额的列的值等于贷项的值和信用行中的列,可以将每个行放到编辑模式,以挂起行值的验证,直到用户尝试提交的值。For example, if you must make sure that the value of the column for a total amount is equal to the values for the debit and credit columns in a row, you can put each row into edit mode to suspend the validation of the row values until the user tries to commit the values.

BeginEdit当用户更改的数据绑定控件,则值隐式调用方法EndEdit调用时,方法隐式调用AcceptChanges方法DataTable对象。The BeginEdit method is called implicitly when the user changes the value of a data-bound control; the EndEdit method is called implicitly when you invoke the AcceptChanges method for the DataTable object. 在此编辑模式,DataRow建议的值将存储原始和新的表示形式。While in this edit mode, the DataRow stores representations of the original and new proposed values. 因此,只要EndEdit尚未调用方法,可以通过传递检索原始或建议版本DataRowVersion.OriginalDataRowVersion.Proposed有关version参数的Item[String, DataRowVersion]属性。Therefore, as long as the EndEdit method has not been called, you can retrieve either the original or proposed version by passing either DataRowVersion.Original or DataRowVersion.Proposed for the version parameter of the Item[String, DataRowVersion] property. 此外可以通过调用取消任何编辑此时CancelEdit方法。You can also cancel any edits at this point by invoking the CancelEdit method.

若要查看行中是否包含原始或建议值,请调用HasVersion方法。To see if the row contains an original or proposed value, call the HasVersion method.

备注

BeginEdit方法会临时挂起RowChanging事件,但delete操作却没有。The BeginEdit method temporarily suspends RowChanging events, but the delete operation does not.

适用于

另请参阅