次の方法で共有


DataRow.BeginEdit メソッド

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

Public Sub BeginEdit()
[C#]
public void BeginEdit();
[C++]
public: void BeginEdit();
[JScript]
public function BeginEdit();

例外

例外の種類 条件
InRowChangingEventException RowChanging イベント内でメソッドが呼び出されました。
DeletedRowInaccessibleException このメソッドは削除した行に対して呼び出されました。

解説

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

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

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

使用例

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

 
Private Sub DemonstrateRowBeginEdit()
    Dim t As New DataTable("table1")
    Dim c As New DataColumn("col1", Type.GetType("System.Int32"))
    AddHandler t.RowChanged, AddressOf Row_Changed
    t.Columns.Add(c)
    ' Add a UniqueConstraint to the table.
    t.Constraints.Add(New UniqueConstraint(c))
    ' Add five rows.
    Dim r As DataRow
       
    Dim i As Integer
    For i = 0 To 4
        ' RowChanged event will occur for every addition.
        r = t.NewRow()
        r(0) = i
        t.Rows.Add(r)
    Next i
    ' AcceptChanges.
    t.AcceptChanges()
    ' Invoke BeginEdit on each.
    Console.WriteLine(ControlChars.Cr _
       + " Begin Edit and print original and proposed values " _
       + ControlChars.Cr)
    Dim myRow As DataRow
    For Each myRow In  t.Rows           
        myRow.BeginEdit()
        myRow(0) = CInt(myRow(0)) + 10
        Console.Write(ControlChars.Tab + " Original " + ControlChars.Tab _
           + myRow(0, DataRowVersion.Original).ToString())
        Console.Write(ControlChars.Tab + " Proposed " + ControlChars.Tab _
           + myRow(0, DataRowVersion.Proposed).ToString() + ControlChars.Cr)
    Next myRow
    Console.WriteLine(ControlChars.Cr)
    ' Accept changes
    t.AcceptChanges()
    ' Change two rows to identical values after invoking BeginEdit.
    t.Rows(0).BeginEdit()
    t.Rows(1).BeginEdit()
    t.Rows(0)(0) = 100
    t.Rows(1)(0) = 100
    Try
        ' Now invoke EndEdit. This will cause the UniqueConstraint
        ' to be enforced.
        t.Rows(0).EndEdit()
        t.Rows(1).EndEdit()
    Catch e As Exception
    ' Process exception and return.
        Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
        log.Source = "My Application"
        log.WriteEntry(e.ToString())
        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 t As DataTable = CType(sender, DataTable)
    Console.WriteLine("RowChanged " + e.Action.ToString() _
       + ControlChars.Tab + e.Row.ItemArray(0).ToString())
End Sub

[C#] 
private void DemonstrateRowBeginEdit(){
   DataTable t = new DataTable("table1");
   DataColumn c = new DataColumn("col1",Type.GetType("System.Int32"));
   t.RowChanged+=new DataRowChangeEventHandler(Row_Changed);
   t.Columns.Add(c);
   // Add a UniqueConstraint to the table.
   t.Constraints.Add(new UniqueConstraint(c));
   // Add five rows.
   DataRow r;
      
   for(int i = 0;i<5; i++){
      // RowChanged event will occur for every addition.
      r= t.NewRow();
      r[0]= i;
      t.Rows.Add(r);
   }
   // AcceptChanges.
   t.AcceptChanges();
   // Invoke BeginEdit on each.
   Console.WriteLine("\n Begin Edit and print original and proposed values \n");
   foreach(DataRow myRow in t.Rows){
      
      myRow.BeginEdit();
      myRow[0]=(int) myRow[0]+10;
      Console.Write("\t Original \t" + myRow[0, DataRowVersion.Original]);
      Console.Write("\t Proposed \t" + myRow[0,DataRowVersion.Proposed] + "\n");
    }
   Console.WriteLine("\n");
   // Accept changes
   t.AcceptChanges();
   // Change two rows to identical values after invoking BeginEdit.
   t.Rows[0].BeginEdit();
   t.Rows[1].BeginEdit();
   t.Rows[0][0]= 100;
   t.Rows[1][0]=100;
   try{
   /* Now invoke EndEdit. This will cause the UniqueConstraint
      to be enforced.*/
      t.Rows[0].EndEdit();
      t.Rows[1].EndEdit();
   }
   catch(Exception e){
          // Process exception and return.
       System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
       log.Source = "My Application";
       log.WriteEntry(e.ToString());
       Console.WriteLine("Exception of type {0} occurred.", e.GetType());
   }
}

private void Row_Changed(object sender, System.Data.DataRowChangeEventArgs e){
   DataTable t = (DataTable)  sender;
   Console.WriteLine("RowChanged " + e.Action.ToString() + "\t" + e.Row.ItemArray[0]);
}

[C++] 
private:
 void DemonstrateRowBeginEdit(){
    DataTable* t = new DataTable(S"table1");
    DataColumn* c = new DataColumn(S"col1",Type::GetType(S"System.Int32"));
    t->RowChanged+=new DataRowChangeEventHandler(this, &Form1::Row_Changed);
    t->Columns->Add(c);
    // Add a UniqueConstraint to the table.
    t->Constraints->Add(new UniqueConstraint(c));
    // Add five rows.
    DataRow* r;
       
    for(int i = 0;i<5; i++){
       // RowChanged event will occur for every addition.
       r= t->NewRow();
       r->Item[0]= __box(i);
       t->Rows->Add(r);
    }
    // AcceptChanges.
    t->AcceptChanges();
    // Invoke BeginEdit on each.
    Console::WriteLine(S"\n Begin Edit and print original and proposed values \n");
    System::Collections::IEnumerator* myEnum = t->Rows->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataRow* myRow = __try_cast<DataRow*>(myEnum->Current);
       
       myRow->BeginEdit();
       myRow->Item[0]= __box(*dynamic_cast<Int32*>(myRow->Item[0])+10);
       Console::Write(S"\t Original \t{0}", myRow->Item[0,DataRowVersion::Original]);
       Console::Write(S"\t Proposed \t{0}\n", myRow->Item[0,DataRowVersion::Proposed]);
     }
    Console::WriteLine(S"\n");
    // Accept changes
    t->AcceptChanges();
    // Change two rows to identical values after invoking BeginEdit.
    t->Rows->Item[0]->BeginEdit();
    t->Rows->Item[1]->BeginEdit();
    t->Rows->Item[0]->Item[0]= __box(100);
    t->Rows->Item[1]->Item[0]= __box(100);
    try{
    /* Now invoke EndEdit. This will cause the UniqueConstraint
       to be enforced.*/
       t->Rows->Item[0]->EndEdit();
       t->Rows->Item[1]->EndEdit();
    }
    catch(Exception* e){
           // Process exception and return.
        System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
        log->Source = S"My Application";
        log->WriteEntry(e->ToString());
        Console::WriteLine(S"Exception of type {0} occurred.", e->GetType());
    }
 }
 
 void Row_Changed(Object* sender, System::Data::DataRowChangeEventArgs* e){
    DataTable* t = dynamic_cast<DataTable*>(sender);
    Console::WriteLine(S"RowChanged {0}\t{1}", __box(e->Action), e->Row->ItemArray[0]);
 }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

DataRow クラス | DataRow メンバ | System.Data 名前空間 | AcceptChanges | CancelEdit | EndEdit | HasVersion | Item | RowState