PC Review


Reply
Thread Tools Rate Thread

DataBinding Control <-> Custom Class

 
 
PeterB
Guest
Posts: n/a
 
      25th Nov 2003
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();
}
}
}
}


 
Reply With Quote
 
 
 
 
Peter Foot [MVP]
Guest
Posts: n/a
 
      25th Nov 2003
If your custom class exposes the IBindingList interface it will have the
appropriate means to signal an event to the control to indicate that data
has changed, the standard controls should all support this method.
http://msdn.microsoft.com/library/de...classtopic.asp

Peter

--
Peter Foot
Windows Embedded MVP
OpenNETCF.org Senior Advisor
www.inthehand.com | www.opennetcf.org

"PeterB" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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();
> }
> }
> }
> }
>
>



 
Reply With Quote
 
David Wrighton [MS]
Guest
Posts: n/a
 
      10th Dec 2003
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" <(E-Mail Removed)>
| 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: <(E-Mail Removed)>
| 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();
| }
| }
| }
| }
|
|
|

 
Reply With Quote
 
PeterB
Guest
Posts: n/a
 
      10th Dec 2003
Yes, I will try it out. Thanks for the help!

/ Peter


"David Wrighton [MS]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)>
> | 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: <(E-Mail Removed)>
> | 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();
> | }
> | }
> | }
> | }
> |
> |
> |
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataBinding custom class to textBox Steve Microsoft Dot NET Framework Forms 2 4th Oct 2005 05:43 PM
DataBinding to Custom Class breaks after New instance: how to get around this? DraguVaso Microsoft Dot NET 3 20th Sep 2005 10:08 AM
Databinding Custom Class =?Utf-8?B?QSBHaWxs?= Microsoft Dot NET Framework Forms 0 27th Jun 2005 11:29 PM
Databinding to custom properties of a custom class in ASP.NET 2 =?Utf-8?B?SW1hciBTcGFhbmphYXJz?= Microsoft ASP .NET 0 20th Apr 2005 08:34 PM
Databinding to custom class Joe Microsoft Dot NET Framework Forms 3 24th Oct 2003 04:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:06 PM.