DataBinding Control <-> Custom Class

P

PeterB

Hi!

I am having trouble with databinding between a custom class and a control. I
use the class as the interface between a textbox and a datasource such as a
database.

In my test application (source below), the idea is that if the user updates
the textbox it will be reflected in the object instantly, and if data is
read from a
datasource into the object (button1 is clicked) it will be reflected in the
controls instantly. The first is no problem. If I change the text in the
textbox the object is updated correctly, but if I change the object propery
value, the textbox text is NOT updated...

I solved this by iterating through all control's databinding lists and call
the underlying BindingManagerBase.ResumeBinding(); This works in the full
framework but ResumeBinding is not supported in the compact framework.

Has anyone any experience with this in the compact framework?

best regards,

Peter

Code:


public class testklass
{
private string m_test;
public testklass()
{
m_test = "First string";
}
public string test
{
get
{
return m_test;
}
set
{
m_test = value;
}
}
}



public class frmDataBinding : System.Windows.Forms.Form
{
private testklass tk;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public frmDataBinding()
{
InitializeComponent();
tk = new testklass();
textBox1.DataBindings.Add("text",tk,"test");
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 64);
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 168);
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(88, 100);
this.textBox2.Text = "textBox2";
//
// Form1
//
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new frmDataBinding());
}
private void button1_Click(object sender, System.EventArgs e)
{
tk.test = "Second String";
foreach (Control ctrl in this.Controls)
{
foreach (Binding db in ctrl.DataBindings)
{
db.BindingManagerBase.ResumeBinding();
}
}
}
}
 
D

David Wrighton [MS]

What you need to do is expose a testChanged event on your object. Otherwise
DataBinding is not able to determine that the value of the property has
changed.

David Wrighton

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| From: "PeterB" <[email protected]>
| Subject: DataBinding Control <-> Custom Class
| Date: Tue, 25 Nov 2003 14:22:17 +0100
| Lines: 136
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: h113n2fls34o264.telia.com 217.208.187.113
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA06.phx.gbl!TK2MSFTNGXA0
5.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:39300
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Hi!
|
| I am having trouble with databinding between a custom class and a
control. I
| use the class as the interface between a textbox and a datasource such as
a
| database.
|
| In my test application (source below), the idea is that if the user
updates
| the textbox it will be reflected in the object instantly, and if data is
| read from a
| datasource into the object (button1 is clicked) it will be reflected in
the
| controls instantly. The first is no problem. If I change the text in the
| textbox the object is updated correctly, but if I change the object
propery
| value, the textbox text is NOT updated...
|
| I solved this by iterating through all control's databinding lists and
call
| the underlying BindingManagerBase.ResumeBinding(); This works in the full
| framework but ResumeBinding is not supported in the compact framework.
|
| Has anyone any experience with this in the compact framework?
|
| best regards,
|
| Peter
|
| Code:
|
|
| public class testklass
| {
| private string m_test;
| public testklass()
| {
| m_test = "First string";
| }
| public string test
| {
| get
| {
| return m_test;
| }
| set
| {
| m_test = value;
| }
| }
| }
|
|
|
| public class frmDataBinding : System.Windows.Forms.Form
| {
| private testklass tk;
| private System.Windows.Forms.TextBox textBox1;
| private System.Windows.Forms.Button button1;
| private System.Windows.Forms.TextBox textBox2;
| /// <summary>
| /// Required designer variable.
| /// </summary>
| private System.ComponentModel.Container components = null;
| public frmDataBinding()
| {
| InitializeComponent();
| tk = new testklass();
| textBox1.DataBindings.Add("text",tk,"test");
| }
| /// <summary>
| /// Clean up any resources being used.
| /// </summary>
| protected override void Dispose( bool disposing )
| {
| if( disposing )
| {
| if (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.textBox1 = new System.Windows.Forms.TextBox();
| this.button1 = new System.Windows.Forms.Button();
| this.textBox2 = new System.Windows.Forms.TextBox();
| //
| // textBox1
| //
| this.textBox1.Location = new System.Drawing.Point(88, 64);
| this.textBox1.Text = "textBox1";
| //
| // button1
| //
| this.button1.Location = new System.Drawing.Point(96, 168);
| this.button1.Text = "button1";
| this.button1.Click += new System.EventHandler(this.button1_Click);
| //
| // textBox2
| //
| this.textBox2.Location = new System.Drawing.Point(88, 100);
| this.textBox2.Text = "textBox2";
| //
| // Form1
| //
| this.ClientSize = new System.Drawing.Size(292, 266);
| this.Controls.Add(this.textBox2);
| this.Controls.Add(this.button1);
| this.Controls.Add(this.textBox1);
| this.Text = "Form1";
| }
| #endregion
| /// <summary>
| /// The main entry point for the application.
| /// </summary>
| static void Main()
| {
| Application.Run(new frmDataBinding());
| }
| private void button1_Click(object sender, System.EventArgs e)
| {
| tk.test = "Second String";
| foreach (Control ctrl in this.Controls)
| {
| foreach (Binding db in ctrl.DataBindings)
| {
| db.BindingManagerBase.ResumeBinding();
| }
| }
| }
| }
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top