DataRow.BeginEdit メソッドとは? わかりやすく解説

DataRow.BeginEdit メソッド

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

名前空間: System.Data
アセンブリ: System.Data (system.data.dll 内)
構文構文

Dim instance As DataRow

instance.BeginEdit
public void BeginEdit ()
public:
void BeginEdit ()
public void BeginEdit ()
例外例外
例外種類条件

InRowChangingEventException

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

DeletedRowInaccessibleException

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

解説解説

DataRow編集モードにするには、BeginEdit メソッド使用します。このモードでは、イベント一時的に中断されるため、ユーザー検証規則トリガせずに 1 つ上のに対して複数変更を行うことができます。たとえば、ある行について合計額の列の値が借方列と貸方列の値に等しいことを確認する必要がある場合各行編集モードにして、ユーザーが値をコミットするまで検証中断できます

ユーザーデータ連結コントロールの値を変更すると、BeginEdit メソッド暗黙的に呼び出されます。DataTable オブジェクトの AcceptChanges メソッド呼び出すと、EndEdit メソッド暗黙的に呼び出されます。編集モードの間は、元の値と新しく提示された値が DataRow格納されています。そのため、EndEdit メソッド呼び出されるまでは、Item プロパティversion パラメータDataRowVersion.Original または DataRowVersion.Proposed を渡すことにより、元のバージョンまたは新しく提示されバージョンいずれか取得できます。この時点編集キャンセルするには、CancelEdit メソッド呼び出します。

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

使用例使用例

この例では、DataColumn オブジェクト 1 つDataRow オブジェクト 5 つ、およびUniqueConstraint 1 つ使用して単純な DataTable作成します。この行の値が変更されるのを監視するための RowChanged イベント ハンドラ追加されます。既存の行に対して BeginEdit呼び出すと、制約イベント一時的に無効になり、元の値と提示した値が出力されます。BeginEdit が再び呼び出されて、2 行を同じ値に設定しますEndEdit呼び出されると、この同一に対して UniqueConstraint強制的に適用されます。

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
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]);
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataRow クラス
DataRow メンバ
System.Data 名前空間
AcceptChanges
CancelEdit
EndEdit
HasVersion
Item
RowState



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「DataRow.BeginEdit メソッド」の関連用語

DataRow.BeginEdit メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataRow.BeginEdit メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS