Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > BindingSource reloads all properties not changed only?
 

BindingSource reloads all properties not changed only?

Hi all,

In the attached example I have simple business object MyTestBO with two properties Seconds (int) and Name (string).

MyTestBO implements INotifyPropertyChanged.

There is a simple WinForm that shows the properties of the business object.

There is also FinWorm.Timer set up for 1 second. On its Tick event it increments the Seconds property on the business object.

Data Source Update Mode for the textbox bindings is OnValidation.

Looks fine. Counter is OK.

If I try to edit the text in the Name text box on every Timer.Tick event it goes back to the original value.

I assume that Binding Source control reloads all properties regardless of what property was changed.

Is there a way to avoid reload of all properties when only one was changed?

Code:

   public class MyTestBO : INotifyPropertyChanged

    {

        public string Name { get; set; }



        int _seconds = 0;

        public int Seconds

        {

            get { return _seconds; }



            set

            {

                _seconds = value;

                if (PropertyChanged != null)

                    PropertyChanged(this, new PropertyChangedEventArgs("Seconds"));

            }

        }



        public event PropertyChangedEventHandler PropertyChanged;

    }

    public partial class BugTest : Form

    {

        public BugTest()

        {

            InitializeComponent();

            myTestBOBindingSource.DataSource = new MyTestBO() { Seconds = 0, Name = "Test Name" };

        }



        private void timer1_Tick(object sender, EventArgs e)

        {

            (myTestBOBindingSource.DataSource as MyTestBO).Seconds++;

        }

    }

<pre lang="x-c#">    //Designer generated code

    partial class BugTest

    {

        /// <summary>



        /// Required designer variable.



        /// </summary>



        private System.ComponentModel.IContainer components = null;



        /// <summary>



        /// Clean up any resources being used.



        /// </summary>



        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>



        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }



        #region Windows Form Designer generated code



        /// <summary>



        /// Required method for Designer support - do not modify



        /// the contents of this method with the code editor.



        /// </summary>



        private void InitializeComponent()

        {

            this.components = new System.ComponentModel.Container();

            this.timer1 = new System.Windows.Forms.Timer(this.components);

            this.textBox1 = new System.Windows.Forms.TextBox();

            this.textBox2 = new System.Windows.Forms.TextBox();

            this.myTestBOBindingSource = new System.Windows.Forms.BindingSource(this.components);

            ((System.ComponentModel.ISupportInitialize)(this.myTestBOBindingSource)).BeginInit();

            this.SuspendLayout();

            // 

            // timer1

            // 

            this.timer1.Enabled = true;

            this.timer1.Interval = 1000;

            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

            // 

            // textBox1

            // 

            this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.myTestBOBindingSource, "Name", true));

            this.textBox1.Location = new System.Drawing.Point(162, 59);

            this.textBox1.Name = "textBox1";

            this.textBox1.Size = new System.Drawing.Size(100, 20);

            this.textBox1.TabIndex = 7;

            // 

            // textBox2

            // 

            this.textBox2.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.myTestBOBindingSource, "Seconds", true));

            this.textBox2.Location = new System.Drawing.Point(48, 59);

            this.textBox2.Name = "textBox2";

            this.textBox2.Size = new System.Drawing.Size(52, 20);

            this.textBox2.TabIndex = 8;

            // 

            // myTestBOBindingSource

            // 

            this.myTestBOBindingSource.DataSource = typeof(TestBug.MyTestBO);

            // 

            // BugTest

            // 

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(311, 194);

            this.Controls.Add(this.textBox2);

            this.Controls.Add(this.textBox1);

            this.Name = "BugTest";

            this.Text = "Form1";

            ((System.ComponentModel.ISupportInitialize)(this.myTestBOBindingSource)).EndInit();

            this.ResumeLayout(false);

            this.PerformLayout();



        }



        private System.Windows.Forms.BindingSource myTestBOBindingSource;

        private System.Windows.Forms.Timer timer1;

        private System.Windows.Forms.TextBox textBox1;

        private System.Windows.Forms.TextBox textBox2;



    }



  • Edited byJNikolov Thursday, August 13, 2009 8:53 AM
  •  
JNikolov  Thursday, August 13, 2009 8:47 AM
The short answer is "no I don't think so".

The longer answer is "why are you wanting to throw change events on every timer tick?"
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Thursday, August 13, 2009 11:27 PM

The way Windows Forms data binding pulls and pushes data values is not as comprehensive as one would like.

Here is a potential workaround to put in your Tick routine prior to updating seconds:

textBox1.DataBindings["Text"].WriteValue();

This syncsthe DataRow object is in sync for that binding.

BTW, thanks for posting complete test code. That helped a lot.

BinaryCoder  Friday, August 14, 2009 2:43 AM

You can use google to search for other answers

Custom Search

More Threads

• Strange DataGridView Exception
• Problem binding a DataTable to a ListBox control
• Datagridview changing row background color
• DateTimePicker problem in DataGridView
• Merging two dataset and updating offline db
• CheckedListBox - how do I get checked items in ItemCheck event
• Dataset being modified??
• download files from server in c#
• Add values using Datatable
• DataGrid.RowState lost...