Export (0) Print
Expand All

DataRow.BeginEdit Method

Starts an edit operation on a DataRow object.

Namespace: System.Data
Assembly: System.Data (in system.data.dll)

public void BeginEdit ()
public void BeginEdit ()
public function BeginEdit ()

Exception typeCondition

InRowChangingEventException

The method was called inside the RowChanging event.

DeletedRowInaccessibleException

The method was called upon a deleted row.

Use the BeginEdit method to put a DataRow into edit mode. In this mode, events are temporarily suspended letting the user make multiple changes to more than one row without triggering validation rules. For example, if you must make sure 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 row into edit mode to suspend the validation of the row values until the user tries 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 AcceptChanges method for the DataTable object. 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 point by invoking the CancelEdit method.

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

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.

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]);


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft