DataRow.BeginEdit 方法

定義

DataRow 物件上開始編輯作業。

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

例外狀況

RowChanging 事件中呼叫該方法。

在刪除的資料列上呼叫該方法。

範例

此範例會建立具有一DataColumn和五DataRow個 對象的簡單 DataTable ,以及 。UniqueConstraint RowChanged當數據列的值變更時,也會新增事件處理程式來監視。 叫用 BeginEdit 現有數據列之後,條件約束和事件會暫時停用,並列印原始和建議的值。 會再次叫用 , BeginEdit 將兩個數據列設定為相同的值。 呼叫 時 EndEdit ,會在 UniqueConstraint 相同的值上強制執行 。

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當您AcceptChangesDataTable用 物件的 方法時,會隱含呼叫 方法。 在此編輯模式中,會 DataRow 儲存原始和新建議值的表示法。 因此,只要 EndEdit 尚未呼叫 方法,您可以傳遞 DataRowVersion.OriginalDataRowVersion.Proposed 屬性 versionItem[] 參數,以擷取原始或建議的版本。 您也可以叫用 方法,取消此時 CancelEdit 的任何編輯。

若要查看數據列是否包含原始或建議的值,請呼叫 HasVersion 方法。

注意

方法 BeginEdit 會暫時暫止 RowChanging 事件,但 delete 作業不會。

適用於

另請參閱