次の方法で共有


DataRow.RejectChanges メソッド

前回 AcceptChanges を呼び出した以降にこの行に対して行われたすべての変更を拒否します。

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

例外

例外の種類 条件
RowNotInTableException 行がテーブルに属していません。

解説

RejectChanges メソッドを呼び出すと、 CancelEdit メソッドが暗黙的に呼び出されて、編集がキャンセルされます。 RowState が Deleted または Modified の場合、行の値は以前の状態に戻り、 RowState は Unchanged になります。 RowState が Added の場合は、行が削除されます。

使用例

[Visual Basic, C#, C++] 2 列と 10 行の単純な DataTable を作成する例を次に示します。 Delete メソッドを使用して複数の DataRow 項目を削除した後、 RejectChanges を呼び出して、削除した行の 1 つの削除を取り消します。

 
Private Sub DemonstrateDeleteRow()
    ' Create a simple DataTable with two columns and ten rows.
    Dim myTable As New DataTable("myTable")
    Dim c1 As New DataColumn("id", Type.GetType("System.Int32"))
    c1.AutoIncrement = True
    Dim c2 As New DataColumn("item", Type.GetType("System.String"))
    myTable.Columns.Add(c1)
    myTable.Columns.Add(c2)
    ' Add ten rows.
    Dim newRow As DataRow
    
    Dim i As Integer
    For i = 0 To 9
        newRow = myTable.NewRow()
        newRow("item") = "Item " + i.ToString()
        myTable.Rows.Add(newRow)
    Next i
    myTable.AcceptChanges()
    ' Create a DataView with the table.
    Dim rc As DataRowCollection = myTable.Rows
    rc(0).Delete()
    rc(2).Delete()
    rc(3).Delete()
    rc(5).Delete()
    Console.WriteLine(rc(3).RowState.ToString())
    ' Reject changes on one deletion.
    rc(3).RejectChanges()
    ' Change the value of the column so it stands out.
    rc(3)("item") = "Deleted, Undeleted, Edited"
    ' Accept changes on others.
    myTable.AcceptChanges()
    ' Print the remaining row values.
    Dim r As DataRow
    For Each r In  myTable.Rows
        Console.WriteLine(r(0).ToString() + ControlChars.Tab + r(1).ToString())
    Next r
End Sub

[C#] 
private void DemonstrateDeleteRow(){
   // Create a simple DataTable with two columns and ten rows.
   DataTable myTable = new DataTable("myTable");
   DataColumn c1 = new DataColumn("id",Type.GetType("System.Int32"));
   c1.AutoIncrement=true;
   DataColumn c2 = new DataColumn("item", Type.GetType("System.String"));
   myTable.Columns.Add(c1);
   myTable.Columns.Add(c2);
   // Add ten rows.
   DataRow newRow;
        
   for(int i = 0; i <10; i++){
      newRow = myTable.NewRow();
      newRow["item"] = "Item " + i;
      myTable.Rows.Add(newRow);
   }
   myTable.AcceptChanges();
   // Create a DataView with the table.
   DataRowCollection rc = myTable.Rows;
   rc[0].Delete();
   rc[2].Delete();
   rc[3].Delete();
   rc[5].Delete();
   Console.WriteLine(rc[3].RowState.ToString());
   // Reject changes on one deletion.
   rc[3].RejectChanges();
   // Change the value of the column so it stands out.
   rc[3]["item"] = "Deleted, Undeleted, Edited";
   // Accept changes on others.
   myTable.AcceptChanges();
   // Print the remaining row values.
   foreach(DataRow r in myTable.Rows){
      Console.WriteLine(r[0] + "\t" + r[1]);
   }
}

[C++] 
private:
 void DemonstrateDeleteRow(){
    // Create a simple DataTable with two columns and ten rows.
    DataTable* myTable = new DataTable(S"myTable");
    DataColumn* c1 = new DataColumn(S"id",Type::GetType(S"System.Int32"));
    c1->AutoIncrement=true;
    DataColumn* c2 = new DataColumn(S"item", Type::GetType(S"System.String"));
    myTable->Columns->Add(c1);
    myTable->Columns->Add(c2);
    // Add ten rows.
    DataRow* newRow;
         
    for(int i = 0; i <10; i++){
       newRow = myTable->NewRow();
       newRow->Item[S"item"] = String::Format( S"Item {0}", __box(i));
       myTable->Rows->Add(newRow);
    }
    myTable->AcceptChanges();
    // Create a DataView with the table.
    DataRowCollection* rc = myTable->Rows;
    rc->Item[0]->Delete();
    rc->Item[2]->Delete();
    rc->Item[3]->Delete();
    rc->Item[5]->Delete();
    Console::WriteLine(rc->Item[3]->RowState);
    // Reject changes on one deletion.
    rc->Item[3]->RejectChanges();
    // Change the value of the column so it stands out.
    rc->Item[3]->Item[S"item"] = S"Deleted, Undeleted, Edited";
    // Accept changes on others.
    myTable->AcceptChanges();
    // Print the remaining row values.
    System::Collections::IEnumerator* myEnum = myTable->Rows->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataRow* r = __try_cast<DataRow*>(myEnum->Current);
       Console::WriteLine(S"{0}\t{1}", r->Item[0], r->Item[1]);
    }
 }

[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 | IsNull