【问题标题】:why GetChanges returns something (when bound to a property) even if there are no changes on datatable?为什么即使数据表没有更改,GetChanges 也会返回一些东西(当绑定到属性时)?
【发布时间】:2009-01-03 04:41:26
【问题描述】:

绑定GetChanges 在绑定到UserControl 的属性时总是返回一些东西(即使是简单的)

我创建了一个UserControl,出于某种我不知道的原因,当我将DataColumn 绑定到我的控件的属性时,dataSet1.GetChanges() 总是返回一些东西,即使绑定到我的控件的列也没有改变。

GetChanges 总是返回一些东西的可能原因是什么?

这里有一个简单的 sn-p 来重现绑定/GetChanges 问题:


using System;

using System.Data;

using System.Windows.Forms;


namespace BindingBug
{

    public partial class Form1 : Form
    {

        DataSet _ds = new DataSet();

        void GetData()
        {           
            var t = new DataTable
            {
                TableName = "beatles",
                Columns =
                {
                    {"lastname", typeof(string)},
                    {"firstname", typeof(string)},
                    {"middlename", typeof(string)}
                }
            };


            t.Rows.Add("Lennon", "John", "Winston");
            t.Rows.Add("McCartney", "James", "Paul");

            _ds.Tables.Add(t);            
        }

        public string Hey { set; get; }

        public Form1()
        {
            InitializeComponent();

            var tLastname = new TextBox { Top = 100 };
            var tFirstname = new TextBox { Top = 130 };

            this.Controls.Add(tLastname);
            this.Controls.Add(tFirstname);

            GetData();


            tLastname.DataBindings.Add("Text", _ds.Tables["beatles"], "lastname");
            tFirstname.DataBindings.Add("Text", _ds.Tables["beatles"], "firstname");

            // if the following line is commented 2nd:Has Changes == false
            this.DataBindings.Add("Hey", _ds.Tables["beatles"], "middlename");


            _ds.AcceptChanges();


            MessageBox.Show("1st:Has Changes = " + _ds.HasChanges().ToString()); 

            var bDetectChanges = new Button { Top = 160, Text = "Detect Changes" };
            bDetectChanges.Click += 
                delegate 
                {
                    this.BindingContext[_ds.Tables["beatles"]].EndCurrentEdit();
                    MessageBox.Show("2nd:Has Changes = " +  (_ds.GetChanges() != null).ToString()); 
                };

            this.Controls.Add(bDetectChanges);

        }    
    }
} //namespace BindingBug

【问题讨论】:

    标签: c# data-binding properties


    【解决方案1】:

    我今天能够解决这个问题,关键是让 BindingContext 的 EndCurrentEdit 知道值是否真的发生了变化。为此,我们必须在我们的控件上实现 System.ComponentModel.INotifypropertychanged。我刚刚从这里看到了解决方案:http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged.aspx

    希望这可以帮助其他人实现他们自己的控件,这些控件在 GetChanges() 上标记错误 change 状态

    public partial class Form1 : Form, System.ComponentModel.INotifyPropertyChanged
    {
        //----------- implements INotifyPropertyChanged -----------
    
    
        // wish C# has this VB.NET's syntactic sugar
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; // implements INotifyPropertyChanged.PropertyChanged 
    
        //----------- start of Form1  ----------
    
    
        DataSet _ds = new DataSet();
    
    
    
        void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    
    
    
        void GetData()
        {
    
            var t = new DataTable
            {
                TableName = "beatles",
                Columns =
                {
                    {"lastname", typeof(string)},
                    {"firstname", typeof(string)},
                    {"middlename", typeof(string)}
                }
            };
    
    
            t.Rows.Add("Lennon", "John", "Winston");
            t.Rows.Add("McCartney", "James", "Paul");
    
            t.Columns["middlename"].DefaultValue = "";
    
            _ds.Tables.Add(t);            
        }
    
    
    
        string _hey = "";
        public string Hey 
        { 
            set 
            {
                if (value != _hey)
                {
                    _hey = value;
                    NotifyPropertyChanged("Hey");
                }
            } 
            get 
            {                
    
                return _hey;  
            } 
        }
    
    
    
        public Form1()
        {
            InitializeComponent();
    
    
    
            var tLastname = new TextBox { Top = 100 };
            var tFirstname = new TextBox { Top = 130 };
    
            this.Controls.Add(tLastname);
            this.Controls.Add(tFirstname);
    
            GetData();
    
    
    
            tLastname.DataBindings.Add("Text", _ds.Tables["beatles"], "lastname");
            tFirstname.DataBindings.Add("Text", _ds.Tables["beatles"], "firstname");
    
            this.DataBindings.Add("Hey", _ds.Tables["beatles"], "middlename");
    
    
            _ds.AcceptChanges();
    
    
    
    
            MessageBox.Show("1st:Has Changes = " + _ds.HasChanges().ToString());
    
            var bDetectChanges = new Button { Top = 160, Text = "Detect Changes" };
            bDetectChanges.Click +=
                delegate
                {
                    this.BindingContext[_ds.Tables["beatles"]].EndCurrentEdit();
                    MessageBox.Show("2nd:Has Changes = " + (_ds.GetChanges() != null).ToString());
    
                };
    
            this.Controls.Add(bDetectChanges);
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 2017-10-06
      • 2013-11-24
      • 2020-05-01
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多