Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataRow.BeginEdit Method

Begins an edit operation on a DataRow object.

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

Exceptions

Exception Type Condition
InRowChangingEventException The method was called inside the RowChanging event.
DeletedRowInaccessibleException The method was called upon a deleted row.

Remarks

Use the BeginEdit method to put a DataRow into edit mode. In this mode, events are temporarily suspended allowing the user to make multiple changes to more than one row without triggering validation rules. For example, if you need to ensure that the value of the column for a total amount is equal to the values for the debit and credit columns in a row, you can put each of the rows into edit mode to suspend the validation of the row values until the user attempts to commit the values.

The BeginEdit method is called implicitly when the user changes the value of a data-bound control; the EndEdit method is called implicitly when you invoke the DataTable object's AcceptChanges method. While in this edit mode, the DataRow stores representations of the original and new proposed values. Therefore, as long as the EndEdit method has not been called, you can retrieve either the original or proposed version by passing either DataRowVersion.Original or DataRowVersion.Proposed for the version parameter of the Item property. You can also cancel any edits at this time by invoking the CancelEdit method.

To see if the row contains an original or proposed value, call the HasVersion method.

Example

[Visual Basic, C#, C++] The example creates a simple DataTable with one DataColumn and five DataRow objects, and a UniqueConstraint. A RowChanged event handler is also added to monitor when the row's value is changing. After invoking BeginEdit on the existing rows, the constraint and event are temporarily disabled and the original and proposed values are printed. The BeginEdit is again invoked to set two rows to the same value. When EndEdit is called, the UniqueConstraint is enforced on the identical values.

[Visual Basic] 
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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataRow Class | DataRow Members | System.Data Namespace | AcceptChanges | CancelEdit | EndEdit | HasVersion | Item | RowState

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft