DataRow.BeginEdit メソッド

定義

DataRow オブジェクトの編集操作を開始します。

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

例外

RowChanging イベント内でメソッドが呼び出されました。

このメソッドは削除した行に対して呼び出されました。

この例では、1 つおよび 5 つのオブジェクトと と を含む単純な DataTable を作成しますUniqueConstraintDataColumnDataRow RowChangedイベント ハンドラーも追加され、行の値が変更されるタイミングを監視します。 既存の行で を BeginEdit 呼び出すと、制約とイベントが一時的に無効になり、元の値と提案された値が出力されます。 BeginEditが再び呼び出され、2 つの行が同じ値に設定されます。 が呼び出されると 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の値を変更すると暗黙的に呼び出されます。メソッドは、オブジェクトの メソッドDataTableを呼び出すときに暗黙的に呼び出AcceptChangesされます。 この編集モードでは、 DataRow 元の値と新しい提案された値の表現が格納されます。 したがって、 メソッドが呼び出されていない限EndEditり、 プロパティの パラメーターItem[]に または を渡すことで、元のバージョンまたはDataRowVersion.Proposedversion提案されたバージョンをDataRowVersion.Original取得できます。 メソッドを呼び出すことで、この時点で編集を CancelEdit 取り消すこともできます。

行に元の値または提案された値が含まれているかどうかを確認するには、 メソッドを HasVersion 呼び出します。

Note

メソッドは BeginEdit イベントを RowChanging 一時的に中断しますが delete 、操作は中断しません。

適用対象

こちらもご覧ください