473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Binding data to form controls

Hi,

I am trying to bind an object to form controls. I've got an object
MyObject which has some properties e.g. ID.

I've got a form, Form1, that contains a textbox ,TextBox1, now I want to
bind the value of the ID-property of a MyObject-object to the TextBox1.

I've tried doing this:

- Form1 has a private field 'private MyObject myObject;'
- Form1 has a public property 'public MyObject MyObject' to get and set
the value of 'myObject'.
- in the Form1 constructor I placed:
this.TextBox1.DataBindings.Add("Text",this.myObjec t,"ID");

I expected this to work, but when I change the value of the MyObject
property of Form1, de value of TextBox1 is not updated to the value of
the new MyObject.ID property.

What is the best way to bind the values of an object's properties to
form controls?

thanks in advance,
Dries
Dec 8 '05 #1
4 2316
See my post titled "Re: Databinding to business object" on the 1st Dec;
basically:

* To get the UI to update based on values, you need to correctly implement
the INotifyPropertyChanged interface
* You may (not 100% sure) get better results binding to a BindingSource
rather than the object directly, and then adding your object to the
BindingSource via a List<T> of your object(s)

Marc

"Dries De Rudder" <dr************@student.kuleuven.be> wrote in message
news:11***************@seven.kulnet.kuleuven.ac.be ...
Hi,

I am trying to bind an object to form controls. I've got an object
MyObject which has some properties e.g. ID.

I've got a form, Form1, that contains a textbox ,TextBox1, now I want to
bind the value of the ID-property of a MyObject-object to the TextBox1.

I've tried doing this:

- Form1 has a private field 'private MyObject myObject;'
- Form1 has a public property 'public MyObject MyObject' to get and set
the value of 'myObject'.
- in the Form1 constructor I placed:
this.TextBox1.DataBindings.Add("Text",this.myObjec t,"ID");

I expected this to work, but when I change the value of the MyObject
property of Form1, de value of TextBox1 is not updated to the value of the
new MyObject.ID property.

What is the best way to bind the values of an object's properties to form
controls?

thanks in advance,
Dries

Dec 8 '05 #2
Re-reading your post, and seeing that it is the *instance* that is changing,
and not properties, then definitely the BindingSource option will help; full
example (which works with both instance and property changes) follows - just
put in a Main method;

The "Reset" button changes the instance; the "Append" button changes a value
on the current instance. Note that this would also work (without changes)
with a form that supports navigation of multiple items - just instead of
completely wiping _source, just .Add() a new member.

Hope this helps,

Marc

public class MyClass : INotifyPropertyChanged {
private string _myField = string.Empty;
public string MyProperty {
get { return _myField; }
set {
if (_myField != value) {
_myField = value;
OnPropertyChanged("MyProperty");
}
}
}
protected void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Form1 : Form {
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button reset, append;
private BindingSource _source;
public Form1() {
// designer init: put a textbox and 2 buttons on the form
InitializeComponent();
// initialise the data-bindings
_source = new BindingSource();
_source.DataSource = typeof(MyClass);
_source.CurrentChanged += new
EventHandler(_source_CurrentChanged);
textBox1.DataBindings.Add("Text", _source, "MyProperty");
// setup events on 2 buttons
reset.Click += delegate { Instance = new MyClass(); };
append.Click+=delegate {Instance.MyProperty+="a";};
// setup an initial object
Instance = new MyClass();
}

void _source_CurrentChanged(object sender, EventArgs e) {
_instance = (MyClass)_source.Current;
}
private MyClass _instance;
public MyClass Instance {
get { return _instance; }
set {
if (!ReferenceEquals(value, _instance)) {
_source.DataSource = new List<MyClass>(new MyClass[] {
value });
}
}
}

private void InitializeComponent() {
textBox1 = new System.Windows.Forms.TextBox();
reset = new System.Windows.Forms.Button();
append = new System.Windows.Forms.Button();
textBox1.Location = new System.Drawing.Point(20, 20);
reset.Location = new System.Drawing.Point(20, 50);
reset.Text = "Reset";
append.Location = new System.Drawing.Point(20, 80);
append.Text = "Append";
Controls.AddRange(new Control[] {textBox1,reset,append});
}
}

"Marc Gravell" <mg******@rm.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
See my post titled "Re: Databinding to business object" on the 1st Dec;
basically:

* To get the UI to update based on values, you need to correctly implement
the INotifyPropertyChanged interface
* You may (not 100% sure) get better results binding to a BindingSource
rather than the object directly, and then adding your object to the
BindingSource via a List<T> of your object(s)

Marc

"Dries De Rudder" <dr************@student.kuleuven.be> wrote in message
news:11***************@seven.kulnet.kuleuven.ac.be ...
Hi,

I am trying to bind an object to form controls. I've got an object
MyObject which has some properties e.g. ID.

I've got a form, Form1, that contains a textbox ,TextBox1, now I want to
bind the value of the ID-property of a MyObject-object to the TextBox1.

I've tried doing this:

- Form1 has a private field 'private MyObject myObject;'
- Form1 has a public property 'public MyObject MyObject' to get and set
the value of 'myObject'.
- in the Form1 constructor I placed:
this.TextBox1.DataBindings.Add("Text",this.myObjec t,"ID");

I expected this to work, but when I change the value of the MyObject
property of Form1, de value of TextBox1 is not updated to the value of
the new MyObject.ID property.

What is the best way to bind the values of an object's properties to form
controls?

thanks in advance,
Dries


Dec 8 '05 #3
The BindingSource works perfect!!

thanks a lot

Marc Gravell wrote:
Re-reading your post, and seeing that it is the *instance* that is changing,
and not properties, then definitely the BindingSource option will help; full
example (which works with both instance and property changes) follows - just
put in a Main method;

The "Reset" button changes the instance; the "Append" button changes a value
on the current instance. Note that this would also work (without changes)
with a form that supports navigation of multiple items - just instead of
completely wiping _source, just .Add() a new member.

Hope this helps,

Marc

public class MyClass : INotifyPropertyChanged {
private string _myField = string.Empty;
public string MyProperty {
get { return _myField; }
set {
if (_myField != value) {
_myField = value;
OnPropertyChanged("MyProperty");
}
}
}
protected void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Form1 : Form {
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button reset, append;
private BindingSource _source;
public Form1() {
// designer init: put a textbox and 2 buttons on the form
InitializeComponent();
// initialise the data-bindings
_source = new BindingSource();
_source.DataSource = typeof(MyClass);
_source.CurrentChanged += new
EventHandler(_source_CurrentChanged);
textBox1.DataBindings.Add("Text", _source, "MyProperty");
// setup events on 2 buttons
reset.Click += delegate { Instance = new MyClass(); };
append.Click+=delegate {Instance.MyProperty+="a";};
// setup an initial object
Instance = new MyClass();
}

void _source_CurrentChanged(object sender, EventArgs e) {
_instance = (MyClass)_source.Current;
}
private MyClass _instance;
public MyClass Instance {
get { return _instance; }
set {
if (!ReferenceEquals(value, _instance)) {
_source.DataSource = new List<MyClass>(new MyClass[] {
value });
}
}
}

private void InitializeComponent() {
textBox1 = new System.Windows.Forms.TextBox();
reset = new System.Windows.Forms.Button();
append = new System.Windows.Forms.Button();
textBox1.Location = new System.Drawing.Point(20, 20);
reset.Location = new System.Drawing.Point(20, 50);
reset.Text = "Reset";
append.Location = new System.Drawing.Point(20, 80);
append.Text = "Append";
Controls.AddRange(new Control[] {textBox1,reset,append});
}
}

"Marc Gravell" <mg******@rm.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
See my post titled "Re: Databinding to business object" on the 1st Dec;
basically:

* To get the UI to update based on values, you need to correctly implement
the INotifyPropertyChanged interface
* You may (not 100% sure) get better results binding to a BindingSource
rather than the object directly, and then adding your object to the
BindingSource via a List<T> of your object(s)

Marc

"Dries De Rudder" <dr************@student.kuleuven.be> wrote in message
news:11***************@seven.kulnet.kuleuven.ac. be...
Hi,

I am trying to bind an object to form controls. I've got an object
MyObject which has some properties e.g. ID.

I've got a form, Form1, that contains a textbox ,TextBox1, now I want to
bind the value of the ID-property of a MyObject-object to the TextBox1.

I've tried doing this:

- Form1 has a private field 'private MyObject myObject;'
- Form1 has a public property 'public MyObject MyObject' to get and set
the value of 'myObject'.
- in the Form1 constructor I placed:
this.TextBox1.DataBindings.Add("Text",this.myOb ject,"ID");

I expected this to work, but when I change the value of the MyObject
property of Form1, de value of TextBox1 is not updated to the value of
the new MyObject.ID property.

What is the best way to bind the values of an object's properties to form
controls?

thanks in advance,
Dries



Dec 8 '05 #4
you're welcome

"Dries De Rudder" <dr************@student.kuleuven.be> wrote in message
news:11***************@seven.kulnet.kuleuven.ac.be ...
The BindingSource works perfect!!

thanks a lot

Marc Gravell wrote:
Re-reading your post, and seeing that it is the *instance* that is
changing, and not properties, then definitely the BindingSource option
will help; full example (which works with both instance and property
changes) follows - just put in a Main method;

The "Reset" button changes the instance; the "Append" button changes a
value on the current instance. Note that this would also work (without
changes) with a form that supports navigation of multiple items - just
instead of completely wiping _source, just .Add() a new member.

Hope this helps,

Marc

public class MyClass : INotifyPropertyChanged {
private string _myField = string.Empty;
public string MyProperty {
get { return _myField; }
set {
if (_myField != value) {
_myField = value;
OnPropertyChanged("MyProperty");
}
}
}
protected void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Form1 : Form {
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button reset, append;
private BindingSource _source;
public Form1() {
// designer init: put a textbox and 2 buttons on the form
InitializeComponent();
// initialise the data-bindings
_source = new BindingSource();
_source.DataSource = typeof(MyClass);
_source.CurrentChanged += new
EventHandler(_source_CurrentChanged);
textBox1.DataBindings.Add("Text", _source, "MyProperty");
// setup events on 2 buttons
reset.Click += delegate { Instance = new MyClass(); };
append.Click+=delegate {Instance.MyProperty+="a";};
// setup an initial object
Instance = new MyClass();
}

void _source_CurrentChanged(object sender, EventArgs e) {
_instance = (MyClass)_source.Current;
}
private MyClass _instance;
public MyClass Instance {
get { return _instance; }
set {
if (!ReferenceEquals(value, _instance)) {
_source.DataSource = new List<MyClass>(new MyClass[]
{ value });
}
}
}

private void InitializeComponent() {
textBox1 = new System.Windows.Forms.TextBox();
reset = new System.Windows.Forms.Button();
append = new System.Windows.Forms.Button();
textBox1.Location = new System.Drawing.Point(20, 20);
reset.Location = new System.Drawing.Point(20, 50);
reset.Text = "Reset";
append.Location = new System.Drawing.Point(20, 80);
append.Text = "Append";
Controls.AddRange(new Control[] {textBox1,reset,append});
}
}

"Marc Gravell" <mg******@rm.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
See my post titled "Re: Databinding to business object" on the 1st Dec;
basically:

* To get the UI to update based on values, you need to correctly
implement the INotifyPropertyChanged interface
* You may (not 100% sure) get better results binding to a BindingSource
rather than the object directly, and then adding your object to the
BindingSource via a List<T> of your object(s)

Marc

"Dries De Rudder" <dr************@student.kuleuven.be> wrote in message
news:11***************@seven.kulnet.kuleuven.ac .be...

Hi,

I am trying to bind an object to form controls. I've got an object
MyObject which has some properties e.g. ID.

I've got a form, Form1, that contains a textbox ,TextBox1, now I want to
bind the value of the ID-property of a MyObject-object to the TextBox1.

I've tried doing this:

- Form1 has a private field 'private MyObject myObject;'
- Form1 has a public property 'public MyObject MyObject' to get and set
the value of 'myObject'.
- in the Form1 constructor I placed:
this.TextBox1.DataBindings.Add("Text",this.myO bject,"ID");

I expected this to work, but when I change the value of the MyObject
property of Form1, de value of TextBox1 is not updated to the value of
the new MyObject.ID property.

What is the best way to bind the values of an object's properties to
form controls?

thanks in advance,
Dries


Dec 8 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marcin Floryan | last post by:
Hello! My question regards opening (and re-opening) Form and the Load event. I have a main form (frmMain) and I also have a data form (frmData). In the main form I have created: Private...
0
by: Ann Morris | last post by:
INTRODUCTION One of the most powerful aspects of .NET and Windows Forms is data binding. Data binding is the process of associating user interface (UI) elements with a data source to generate a...
1
by: JD Kronicz | last post by:
Hi .. I have an issue I have been beating my head against the wall on for some time. I am trying to use late binding for MS graph so that my end users don't have to worry about having the right...
0
by: popsovy | last post by:
Hi I have a question about whether Data Binding can facilitate the process of saving data in a web application I learned that you can data bind information from a number of different data...
5
by: Vigneshwar Pilli via DotNetMonster.com | last post by:
string connectionString1 = "server=(local); user=sa;password=sa; database=sonic"; System.Data.SqlClient.SqlConnection dbConnection1 = new System.Data.SqlClient.SqlConnection(connectionString1);...
11
by: Rourke Eleven | last post by:
I have looked and searched. What good is the databind property on Radiobuttons? How does one go about actually using it? What is a good resource on this? I understand that I can easily get/set...
0
by: Larry Serflaten | last post by:
I am not sure how many are aware of this sort of data binding, but as it is new to many (classic) VB developers I thought I would post this once just to let people know of its availablility. ...
2
by: Matthias | last post by:
Hi Team this may be a newbie question. I have searched the discussions before posting: I'd like to re-use a form instance to edit record details. My data binding (a DataTable bound to...
19
by: Larry Lard | last post by:
In the old days (VB3 era), there was a thing called the Data Control, and you could use it to databind controls on forms to datasources, and so (as the marketing speak goes), 'create database...
3
by: Simon Tamman | last post by:
I've come across an interesting bug. I have workarounds but i'd like to know the root of the problem. I've stripped it down into a short file and hope someone might have an idea about what's going...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.