IEditableObject 接口

提供提交或回滚对用作数据源的对象所做更改的功能。

**命名空间:**System.ComponentModel
**程序集:**System(在 system.dll 中)

语法

声明
Public Interface IEditableObject
用法
Dim instance As IEditableObject
public interface IEditableObject
public interface class IEditableObject
public interface IEditableObject
public interface IEditableObject

备注

该接口通常用于捕获 DataRowViewBeginEditEndEditCancelEdit 语义。

示例

下面的示例提供 IEditableObject 接口的简单实现。Customer 类存储客户信息并可用作客户数据库的集合。本示例假定您已经使用了 CustomerList 类,该类可以在 IBindingList 类中的示例中找到。

Public Class Customer
    Implements IEditableObject

    Structure CustomerData
        Friend id As String
        Friend firstName As String
        Friend lastName As String
    End Structure 

    Public parent As CustomersList
    Private custData As CustomerData
    Private backupData As CustomerData
    Private inTxn As Boolean = False


    ' Implements IEditableObject
    Sub BeginEdit() Implements IEditableObject.BeginEdit
        Console.WriteLine("Start BeginEdit")
        If Not inTxn Then
            Me.backupData = custData
            inTxn = True
            Console.WriteLine(("BeginEdit - " + Me.backupData.lastName))
        End If
        Console.WriteLine("End BeginEdit")
    End Sub 


    Sub CancelEdit() Implements IEditableObject.CancelEdit
        Console.WriteLine("Start CancelEdit")
        If inTxn Then
            Me.custData = backupData
            inTxn = False
            Console.WriteLine(("CancelEdit - " + Me.custData.lastName))
        End If
        Console.WriteLine("End CancelEdit")
    End Sub 


    Sub EndEdit() Implements IEditableObject.EndEdit
        Console.WriteLine(("Start EndEdit" + Me.custData.id + Me.custData.lastName))
        If inTxn Then
            backupData = New CustomerData()
            inTxn = False
            Console.WriteLine(("Done EndEdit - " + Me.custData.id + Me.custData.lastName))
        End If
        Console.WriteLine("End EndEdit")
    End Sub 


    Public Sub New(ByVal ID As String)
        Me.custData = New CustomerData()
        Me.custData.id = ID
        Me.custData.firstName = ""
        Me.custData.lastName = ""
    End Sub 


    Public ReadOnly Property ID() As String
        Get
            Return Me.custData.id
        End Get
    End Property


    Public Property FirstName() As String
        Get
            Return Me.custData.firstName
        End Get
        Set(ByVal Value As String)
            Me.custData.firstName = Value
            Me.OnCustomerChanged()
        End Set
    End Property


    Public Property LastName() As String
        Get
            Return Me.custData.lastName
        End Get
        Set(ByVal Value As String)
            Me.custData.lastName = Value
            Me.OnCustomerChanged()
        End Set
    End Property


    Friend Property Parents() As CustomersList
        Get
            Return Parent
        End Get
        Set(ByVal Value As CustomersList)
            parent = Value
        End Set
    End Property


    Private Sub OnCustomerChanged()
        If Not inTxn And Not (Parent Is Nothing) Then
            Parent.CustomerChanged(Me)
        End If
    End Sub 

    
    Public Overrides Function ToString() As String
        Dim sb As New StringWriter()
        sb.Write(Me.FirstName)
        sb.Write(" ")
        sb.Write(Me.LastName)
        Return sb.ToString()
    End Function 
End Class
public class Customer : IEditableObject 
{
   
    struct CustomerData 
    {
        internal string id ;
        internal string firstName ;
        internal string lastName ;
    }
    
    private CustomersList parent;
    private CustomerData custData; 
    private CustomerData backupData; 
    private bool inTxn = false;

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

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

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

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

    public string ID 
    {
        get 
        {
            return this.custData.id;
        }
    }
    
    public string FirstName 
    {
        get 
        {
            return this.custData.firstName;
        }
        set 
        {
            this.custData.firstName = value;
            this.OnCustomerChanged();
        }
    }
         
    public string LastName 
    {
        get 
        {
            return this.custData.lastName;
        }
        set 
        {
            this.custData.lastName = value;
            this.OnCustomerChanged();
        }
    }
   
    internal CustomersList Parent 
    {
        get 
        {
            return parent;
        }
        set 
        {
            parent = value ;
        }
    }

    private void OnCustomerChanged() 
    {
        if (!inTxn && Parent != null) 
        {
            Parent.CustomerChanged(this);
        }
    }
    
    public override string ToString() 
    {
        StringWriter sb = new StringWriter();
        sb.Write(this.FirstName);
        sb.Write(" ");
        sb.Write(this.LastName);
        return sb.ToString();
    }   
}

平台

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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

IEditableObject 成员
System.ComponentModel 命名空间