none
关于bindingsource类的CancelEdit()方法 RRS feed

  • 常规讨论

  • 请教各位,我在窗体上用bindingsource的datasource赋值为一个List<T>的集合,当我点击添加的时候,把这个List里面添加一行,我调用BindingSource的CancelEdit()方法时,确不能取消更改,这是怎么回事啊,如果要调用BindingSource的CancelEdit()方法是不是要用别的数据源
    crazyBasketball
    2009年12月3日 5:56

全部回复

  • 这个方法是需要条件的  http://msdn.microsoft.com/zh-cn/library/system.windows.forms.bindingsource.canceledit(VS.80).aspx

    如果满足下面的两个条件,此方法将会丢弃自上次保存或加载操作之后对数据的修改:


    Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~
    2009年12月3日 6:41
    版主
  • 我实现了那个接口,但是好像还是不可以哦


    crazyBasketball
    2009年12月3日 8:32
  • 你好!
         数据源包含的对象实现了这个接口才可以,也就是你的那个T需要实现这个接口,具体参考这个实例:
    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();
        }   
    }

    周雪峰
    2009年12月3日 9:33
    版主