perché GetChanges restituisce qualcosa (se associato a una proprietà) anche se su Datatable non ci sono cambiamenti?

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

  •  03-07-2019
  •  | 
  •  

Domanda

Associato GetChanges restituisce sempre qualcosa quando associato a una proprietà di UserControl (anche su una semplice)

Ho creato un UserControl , per qualche ragione a mia insaputa, quando ho associato un DataColumn alla proprietà del mio controllo dataSet1.GetChanges () restituisce sempre qualcosa, anche la colonna associata al mio controllo non è stata modificata.

Qual è la possibile causa per cui GetChanges restituisce sempre qualcosa?

Ecco un semplice frammento per riprodurre il problema di associazione / 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
È stato utile?

Soluzione

Sono stato in grado di risolvere il problema oggi, la cosa fondamentale è rendere consapevole EndCurrentEdit di BindingContext se il valore realmente è cambiato. per questo, dobbiamo implementare System.ComponentModel.INotifyproperty modificato sul nostro controllo. ho appena visto la soluzione da qui: http: // msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged.aspx

spero che questo possa aiutare gli altri a implementare i propri controlli che segnalano lo stato change errato su 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);

    }

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top