왜 GetChanges가 DataTable에 변경 사항이 없더라도 (재산에 묶일 때) 무언가를 반환합니까?

StackOverflow https://stackoverflow.com/questions/408607

  •  03-07-2019
  •  | 
  •  

문제

경계 GetChanges 항상 묶을 때는 무언가를 반환합니다 UserControl의 속성 (간단한 속성)

나는 만들었다 UserControl, 어떤 이유로 나에게 알려지지 않은데, 내가 묶을 때 DataColumn 내 통제의 속성에 dataSet1.GetChanges() 항상 무언가를 반환하십시오. 내 제어에 바인딩 된 열도 변경되지 않았습니다.

가능한 원인은 무엇입니까? GetChanges 항상 뭔가를 돌려 주나요?

바인딩/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
도움이 되었습니까?

해결책

오늘 문제를 해결할 수 있었는데, 핵심적인 점은 BindingContext의 endCurrentEdit을 값을 인식하게하는 것입니다. 진짜 변경. 이를 위해서는 제어에서 System.componentModel.inotifyPropertyChanged를 구현해야합니다. 방금 여기에서 해결책을 보았습니다. http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged.aspx

이것이 다른 사람들이 자신의 컨트롤을 구현하는 데 도움이되기를 바랍니다. 변화 getchanges의 상태 ()

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

    }

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top