DataRow.BeginEdit 方法

定義

DataRow 物件上開始編輯作業。Starts an edit operation on a DataRow object.

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

例外狀況

RowChanging 事件中呼叫該方法。The method was called inside the RowChanging event.

在刪除的資料列上呼叫該方法。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 時,會在相同的值上強制執行 UniqueConstraintWhen 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 方法;當您叫用 DataTable 物件的 AcceptChanges 方法時,會隱含地呼叫 EndEdit 方法。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 方法,您就可以針對 Item[String, DataRowVersion] 屬性的 version 參數傳遞 DataRowVersion.OriginalDataRowVersion.Proposed,藉此抓取原始或建議的版本。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.

適用於

另請參閱