IEditableObject Interface

Definition

Provides functionality to commit or rollback changes to an object that is used as a data source.

C#
public interface IEditableObject
Derived

Examples

The following sample provides a simple implementation of the IEditableObject interface. The Customer class stores customer information and can be used as a collection for a customer database. This sample assumes that you have used the CustomerList class that can be found in sample in the IBindingList class.

C#
public class Customer : IEditableObject
{
    struct CustomerData
    {
        internal string id;
        internal string firstName;
        internal string lastName;
    }

    CustomerData custData;
    CustomerData backupData;
    bool inTxn;

    // Implements IEditableObject
    void IEditableObject.BeginEdit()
    {
        Console.WriteLine("Start BeginEdit");
        if (!inTxn)
        {
            backupData = custData;
            inTxn = true;
            Console.WriteLine("BeginEdit - " + backupData.lastName);
        }
        Console.WriteLine("End BeginEdit");
    }

    void IEditableObject.CancelEdit()
    {
        Console.WriteLine("Start CancelEdit");
        if (inTxn)
        {
            custData = backupData;
            inTxn = false;
            Console.WriteLine("CancelEdit - " + custData.lastName);
        }
        Console.WriteLine("End CancelEdit");
    }

    void IEditableObject.EndEdit()
    {
        Console.WriteLine("Start EndEdit" + custData.id + custData.lastName);
        if (inTxn)
        {
            backupData = new CustomerData();
            inTxn = false;
            Console.WriteLine("Done EndEdit - " + custData.id + custData.lastName);
        }
        Console.WriteLine("End EndEdit");
    }

    public Customer(string ID) : base() => custData = new CustomerData
    {
        id = ID,
        firstName = "",
        lastName = ""
    };

    public string ID => custData.id;

    public string FirstName
    {
        get => custData.firstName;
        set
        {
            custData.firstName = value;
            OnCustomerChanged();
        }
    }

    public string LastName
    {
        get => custData.lastName;
        set
        {
            custData.lastName = value;
            OnCustomerChanged();
        }
    }

    internal CustomersList Parent { get; set; }

    void OnCustomerChanged()
    {
        if (!inTxn && Parent != null)
        {
            Parent.CustomerChanged(this);
        }
    }

    public override string ToString()
    {
        StringWriter sb = new();
        sb.Write(FirstName);
        sb.Write(" ");
        sb.Write(LastName);
        return sb.ToString();
    }
}

Remarks

This interface is typically used to capture the BeginEdit, EndEdit, and CancelEdit semantics of a DataRowView.

Methods

BeginEdit()

Begins an edit on an object.

CancelEdit()

Discards changes since the last BeginEdit() call.

EndEdit()

Pushes changes since the last BeginEdit() or AddNew() call into the underlying object.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0