Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Another DataBindings.Add() isse
Message
De
18/01/2006 00:23:29
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
01087761
Message ID:
01087843
Vues:
9
This message has been marked as the solution to the initial question of the thread.
Yes, I ran your code and it certainly behaved the way you described it ... and it's because of the way you've done your properties, having their get/set manipulate the .Value of the control. That's not really how you should databind controls.

So, here's how I changed your code (I kept your old code in there, I just commented it out). The difference is that you had the UserControl trying to bind itself to it's own properties and to another class's properties. What I did was to create a DataBind() method on the UserControl that does the DataBinding of the CheckBox and NumericUpDown controls the correct way:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication14
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		#region Declarations

		private WindowsApplication14.UserControl1 userControl11;
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button button1;

		private Class1 class1 = new Class1();

		#endregion

		#region Constructor / Destructor

		public Form1()
		{
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#endregion

		#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.userControl11 = new WindowsApplication14.UserControl1();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// userControl11
			// 
			this.userControl11.Location = new System.Drawing.Point(24, 24);
			this.userControl11.N1 = new System.Decimal(new int[] {
																	 0,
																	 0,
																	 0,
																	 0});
			this.userControl11.N2 = new System.Decimal(new int[] {
																	 0,
																	 0,
																	 0,
																	 0});
			this.userControl11.Name = "userControl11";
			this.userControl11.Size = new System.Drawing.Size(152, 144);
			this.userControl11.TabIndex = 0;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(56, 184);
			this.button1.Name = "button1";
			this.button1.TabIndex = 1;
			this.button1.Text = "Display";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(272, 230);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.userControl11);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		#region Events
		
		private void Form1_Load(object sender, System.EventArgs e)
		{
			this.userControl11.DataBindings.Clear();
			this.userControl11.DataBind(this.class1);
//			this.userControl11.DataBindings.Add("N1", this.class1, "N1");
//			this.userControl11.DataBindings.Add("N2", this.class1, "N2");
//			this.userControl11.DataBindings.Add("C1", this.class1, "C1");
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show("N1:"+this.class1.N1.ToString() + Environment.NewLine + "N2:"+this.class1.N2.ToString() + Environment.NewLine+ "C1:"+this.class1.C1.ToString());
		}	

		#endregion

	}


	public class UserControl1 : System.Windows.Forms.UserControl
	{
		#region Declarations

		private decimal n1;
		private decimal n2;
		private bool    c1;

		private System.Windows.Forms.NumericUpDown numericUpDown1;
		private System.Windows.Forms.NumericUpDown numericUpDown2;
		private System.Windows.Forms.CheckBox checkBox1;
		private System.ComponentModel.Container components = null;

		#endregion

		#region Properties

		public decimal N1
		{
			get{return this.n1;}
			set{this.n1 = value;}
//			get{return this.numericUpDown1.Value;}
//			set{this.numericUpDown1.Value = value;}
		}

		public decimal N2
		{
			get{return this.n2;}
			set{this.n2 = value;}
//			get{return this.numericUpDown2.Value;}
//			set{this.numericUpDown2.Value = value;}
		}

		public bool C1
		{
			get{return this.c1;}
			set{this.c1 = value;}
//			get{return this.checkBox1.Checked;}
//			set{this.checkBox1.Checked = value;}
		}

		#endregion

		#region Constructor / Destructor

		public UserControl1()
		{
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#endregion

		#region Component 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.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
			this.numericUpDown2 = new System.Windows.Forms.NumericUpDown();
			this.checkBox1 = new System.Windows.Forms.CheckBox();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown2)).BeginInit();
			this.SuspendLayout();
			// 
			// numericUpDown1
			// 
			this.numericUpDown1.Location = new System.Drawing.Point(40, 32);
			this.numericUpDown1.Name = "numericUpDown1";
			this.numericUpDown1.Size = new System.Drawing.Size(56, 20);
			this.numericUpDown1.TabIndex = 0;
			// 
			// numericUpDown2
			// 
			this.numericUpDown2.Location = new System.Drawing.Point(40, 72);
			this.numericUpDown2.Name = "numericUpDown2";
			this.numericUpDown2.Size = new System.Drawing.Size(56, 20);
			this.numericUpDown2.TabIndex = 1;
			// 
			// checkBox1
			// 
			this.checkBox1.Location = new System.Drawing.Point(8, 8);
			this.checkBox1.Name = "checkBox1";
			this.checkBox1.TabIndex = 2;
			this.checkBox1.Text = "checkBox1";
			// 
			// UserControl1
			// 
			this.Controls.Add(this.checkBox1);
			this.Controls.Add(this.numericUpDown2);
			this.Controls.Add(this.numericUpDown1);
			this.Name = "UserControl1";
			this.Size = new System.Drawing.Size(152, 144);
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown2)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		#region Methods

		public void DataBind(object BindTo)
		{
			this.DataBindings.Clear();

			if (BindTo is Class1)
			{
				Class1 o = (Class1)BindTo;

				this.numericUpDown1.DataBindings.Add("Value", o, "N1");
				this.numericUpDown2.DataBindings.Add("Value", o, "N2");
				this.checkBox1.DataBindings.Add("Checked", o, "C1");
			}
		}

		#endregion
	}


	public class Class1
	{
		private decimal n1 = 1;
		public decimal N1
		{
			get{return this.n1;}
			set{this.n1 = value;}
		}
		private decimal n2 = 2;
		public decimal N2
		{
			get{return this.n2;}
			set{this.n2 = value;}
		}

		private bool c1 = true;
		public bool C1
		{
			get{return this.c1;}
			set{this.c1 = value;}
		}
	}


}
You can expand on this a bit, like maybe make the Class1 implement an Interface that the UserControl binds to instead. In that case, your UserControl's DataBind() method might look like this (this should work, but I haven't tested it):
		public void DataBind(object BindTo)
		{
			this.DataBindings.Clear();

			if (BindTo is IMyBindingClass)
			{
				Class1 o = (IMyBindingClass))BindTo;

				this.numericUpDown1.DataBindings.Add("Value", o, "N1");
				this.numericUpDown2.DataBindings.Add("Value", o, "N2");
				this.checkBox1.DataBindings.Add("Checked", o, "C1");
			}
		}
~~Bonnie

>Bonnie,
>Thanks for your reply. I think I kinda understand why what is happening is happening, but I don't have to like it :)
>I must say I didn't expect it to behave like it did.
>Did you try to run the code to see what happen?
>Do you know of a workaround? (I have a work around in mind but I don't like it <s>)
>
>Einar
>
>>Einar,
>>
>>That's because I don't think that's the correct way to databind this stuff. I'll post an explanation later tonite along with some code ... I can't do it now because I'm about ready to close up shop here and head for home.
>>
>>~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform