473,668 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about Databinding - binding controls to members of my business object.

Let's suppose I have an instance of MyClass, which has the properties
MyText1 and MyText2, and sets the values of MyText1 and MyText2 to 'A'
and 'B' in it's constructor.

Let's also suppose I have a Form with textBox1, textBox2, btnShowValue,
and btnNew.

Let's also suppose I have this code.

//member of Form
MyClass MyObject;

//in Form Load Method:
{
MyObject = new MyClass();

textBox1.DataBi ndings.Add("Tex t", MyObject, "MyText1");
textBox2.DataBi ndings.Add("Tex t", MyObject, "MyText2");
}

//in btnShowValue Click Method
{
MessageBox.Show (MyObject.MyTex t1);
MessageBox.Show (MyObject.MyTex t2);
}

//in btnNew Click Method
{
MyObject = new MyClass();
}
If I were to run this program, I could start typing into Textbox1 and
Textbox2. Then if I were to click btnShowValue, I would see
MessageBoxes with the values of what I just typed, being as the
textboxes are bound to the properties of the object.

BUT, If I were to click btnNew, I would expect Textbox1 and Textbox2 to
revert to the default values 'A' and 'B'.

They do not. They stay the same. And clicking btnShowValues gives you
'A' and 'B' from now on, no matter what you type in the textboxes.

My question is WHY? What am I missing here?
and How do i alter this code so that it works the way I want it to?

Jan 9 '06 #1
13 2404
Michael,

When you pass the MyObject reference to the Add method, it will remember
the reference, but it knows nothing about the variable or when it changes.

What you would have to do is remove the binding, and then add a new one,
with a reference to the new class instance.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<Mi************ @gmail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Let's suppose I have an instance of MyClass, which has the properties
MyText1 and MyText2, and sets the values of MyText1 and MyText2 to 'A'
and 'B' in it's constructor.

Let's also suppose I have a Form with textBox1, textBox2, btnShowValue,
and btnNew.

Let's also suppose I have this code.

//member of Form
MyClass MyObject;

//in Form Load Method:
{
MyObject = new MyClass();

textBox1.DataBi ndings.Add("Tex t", MyObject, "MyText1");
textBox2.DataBi ndings.Add("Tex t", MyObject, "MyText2");
}

//in btnShowValue Click Method
{
MessageBox.Show (MyObject.MyTex t1);
MessageBox.Show (MyObject.MyTex t2);
}

//in btnNew Click Method
{
MyObject = new MyClass();
}
If I were to run this program, I could start typing into Textbox1 and
Textbox2. Then if I were to click btnShowValue, I would see
MessageBoxes with the values of what I just typed, being as the
textboxes are bound to the properties of the object.

BUT, If I were to click btnNew, I would expect Textbox1 and Textbox2 to
revert to the default values 'A' and 'B'.

They do not. They stay the same. And clicking btnShowValues gives you
'A' and 'B' from now on, no matter what you type in the textboxes.

My question is WHY? What am I missing here?
and How do i alter this code so that it works the way I want it to?

Jan 9 '06 #2
An alternative to the above is to bind not the the object, but to a
BindingSource; this acts as a proxy between the UI and one-or-more objects
(potentially allowing cursor navigation, but that's an aside for this
scenario).

All you then do is change the object in the BindingSource, leaving you to do
your bindings in the designer (if you so choose).

I posted a full working example of this to this group previously - 8th Dec
2005, titled "Re: Binding data to form controls".

Marc

<Mi************ @gmail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Let's suppose I have an instance of MyClass, which has the properties
MyText1 and MyText2, and sets the values of MyText1 and MyText2 to 'A'
and 'B' in it's constructor.

Let's also suppose I have a Form with textBox1, textBox2, btnShowValue,
and btnNew.

Let's also suppose I have this code.

//member of Form
MyClass MyObject;

//in Form Load Method:
{
MyObject = new MyClass();

textBox1.DataBi ndings.Add("Tex t", MyObject, "MyText1");
textBox2.DataBi ndings.Add("Tex t", MyObject, "MyText2");
}

//in btnShowValue Click Method
{
MessageBox.Show (MyObject.MyTex t1);
MessageBox.Show (MyObject.MyTex t2);
}

//in btnNew Click Method
{
MyObject = new MyClass();
}
If I were to run this program, I could start typing into Textbox1 and
Textbox2. Then if I were to click btnShowValue, I would see
MessageBoxes with the values of what I just typed, being as the
textboxes are bound to the properties of the object.

BUT, If I were to click btnNew, I would expect Textbox1 and Textbox2 to
revert to the default values 'A' and 'B'.

They do not. They stay the same. And clicking btnShowValues gives you
'A' and 'B' from now on, no matter what you type in the textboxes.

My question is WHY? What am I missing here?
and How do i alter this code so that it works the way I want it to?

Jan 10 '06 #3
Thanks for the responses.

It appears as though the BindingSource is new to 2005. My project uses
a .Net API that was created in 1.1 and not compatible in a 2.0 project,
therefore restricting me to use VS 2003.

Anything comparable to a BindingSource in 2003?

Jan 10 '06 #4
Not as far as I know. IIRC, you didn't state 1.1 in the original question so
I assumed 2.0; in this case you have to do it the hard way (as per the other
poster).

Personally, my recommendation is that it is better to invest time making an
app 2005 compatible than to invest time working around 2003 limitations that
are addressed (often very gracefully) in 2005 - however, I appreciate that
this is not always achievable, particularly if you only want to make a minor
change.

Marc

<Mi************ @gmail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Thanks for the responses.

It appears as though the BindingSource is new to 2005. My project uses
a .Net API that was created in 1.1 and not compatible in a 2.0 project,
therefore restricting me to use VS 2003.

Anything comparable to a BindingSource in 2003?

Jan 10 '06 #5
Appreciate the feedback.

According to these responses, these are the changes I've made:

//in Form Load Method:
{
MyObject = new MyClass();

SetDataBindings ();
}

//in btnNew Click Method
{
MyObject = new MyClass();

SetDataBindings ();
}

//in SetDataBindings Method
{
foreach(Control ctrl in this.Controls)
{
ctrl.DataBindin gs.Clear();
}
textBox1.DataBi ndings.Add("Tex t", MyObject, "MyText1");
textBox2.DataBi ndings.Add("Tex t", MyObject, "MyText2");
}

So that works great and eliminates my initial problem. Thanks!
But here is another issue...
lets say I add another button. In it's click event method, I have this
code:
{
MyObject.MyText 1 = "C";
MyObject.MyText 2 = "D";
}

I would expect the values in the textBoxes to change upon clicking this
button. But they don't!
However, if I start typing into textBox1 then place my cursor in
textBox2, textBox2 all of a sudden displays "D".

So somehow, the application knew to change the value in at least one of
the textBoxes, but only after changing one textbox and putting my
cursor the other.
Why is this? How can I get my application to know to change the values
in both textboxes immediately upon updating MyText1 and MyText2?

Jan 10 '06 #6
This illustrates my previous point about 1.1 vs 2.0.

The "simple" answer (from my external viewpoint) is to move to 2.0 and
implement the INotifyProperty Changed interface (ammending your setters
accordingly). I don't know of a good way to do this in 1.1, short of cloning
the INotifyProperty Changed interface and using it to reset your bindings
manually (i.e. catch the event and reset your bindings for the object). This
approach would at least smooth the transition from 1.1 to 2.0, as you would
just discard your local version of INotifyProperty Changed and your event
handler, and hopefully things would continue to just work.

The INotifyProperty Changed interface is designed to deal with *exactly* this
scenario in a generic manner, and integrates tightly into bindings.

Marc

<Mi************ @gmail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Appreciate the feedback.

According to these responses, these are the changes I've made:

//in Form Load Method:
{
MyObject = new MyClass();

SetDataBindings ();
}

//in btnNew Click Method
{
MyObject = new MyClass();

SetDataBindings ();
}

//in SetDataBindings Method
{
foreach(Control ctrl in this.Controls)
{
ctrl.DataBindin gs.Clear();
}
textBox1.DataBi ndings.Add("Tex t", MyObject, "MyText1");
textBox2.DataBi ndings.Add("Tex t", MyObject, "MyText2");
}

So that works great and eliminates my initial problem. Thanks!
But here is another issue...
lets say I add another button. In it's click event method, I have this
code:
{
MyObject.MyText 1 = "C";
MyObject.MyText 2 = "D";
}

I would expect the values in the textBoxes to change upon clicking this
button. But they don't!
However, if I start typing into textBox1 then place my cursor in
textBox2, textBox2 all of a sudden displays "D".

So somehow, the application knew to change the value in at least one of
the textBoxes, but only after changing one textbox and putting my
cursor the other.
Why is this? How can I get my application to know to change the values
in both textboxes immediately upon updating MyText1 and MyText2?

Jan 10 '06 #7
Yeah, unfortunately I can't move to 2.0 as the 3rd party .NET API I am
using isn't compatible. But this is good stuff. I'm sure I will
eventually come across this again in 2005, in which case I will
definately keep INotifyProperty Changed in mind.

I still feel like there is still something else I can do here.
Something gets triggered when I change one textBox and put the cursor
in the other. If I can only figure out what it is... Then I could
trigger my own PropertyChanged method in all of the "set" portions of
my properties, which could then trigger an event that could tell the
textBoxes that they need to update.

Jan 10 '06 #8
Hi,

<Mi************ @gmail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Appreciate the feedback.

According to these responses, these are the changes I've made:

//in Form Load Method:
{
MyObject = new MyClass();

SetDataBindings ();
}

//in btnNew Click Method
{
MyObject = new MyClass();

SetDataBindings ();
}

//in SetDataBindings Method
{
foreach(Control ctrl in this.Controls)
{
ctrl.DataBindin gs.Clear();
}
textBox1.DataBi ndings.Add("Tex t", MyObject, "MyText1");
textBox2.DataBi ndings.Add("Tex t", MyObject, "MyText2");
}

So that works great and eliminates my initial problem. Thanks!
But here is another issue...
lets say I add another button. In it's click event method, I have this
code:
{
MyObject.MyText 1 = "C";
MyObject.MyText 2 = "D";
}

If you're using NET1.1 you need to have similar named events for each
property (PropertyName+" Changed"), then the UI will refresh when a property
changes, eg.:

public class MyClass
{
private string myText1 = "";

public EventHandler MyText1Changed; //it needs to be EventHandler

public string MyText1
{
get { return myText1; }
set
{
if ( myText1 != value )
{
myText1 = value;
if ( MyText1Changed! =null )
MyText1Changed( this, EventArgs.Empty );
}
}

}

HTH,
Greetings

I would expect the values in the textBoxes to change upon clicking this
button. But they don't!
However, if I start typing into textBox1 then place my cursor in
textBox2, textBox2 all of a sudden displays "D".

So somehow, the application knew to change the value in at least one of
the textBoxes, but only after changing one textbox and putting my
cursor the other.
Why is this? How can I get my application to know to change the values
in both textboxes immediately upon updating MyText1 and MyText2?

Jan 10 '06 #9
Thanks for the info! But this alone didn't solve my problem.

Adding this code gave me a nice MyText1Changed Event. But just having
the event didn't update the UI.
I need the code that will basically tell the textBoxes:
"Hey, the data you are binded to has changed! Update yourselves and
display your new Text values!"

If I had that code, I could certainly put it in the method for my
MyText1Changed event.

I am assuming that there is some way of doing this based on the fact
that if I change textBox1 and put the cursor textBox2, textBox2 all of
a sudden diplays it's new Text value. What is making it display it's
new value? That is what I need. A way of accessing and triggering
whatever it is that makes textBox2 display it's new Text value.

Thanks for the help so far. I feel like the solution is almost there.

Jan 10 '06 #10

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

Similar topics

15
3675
by: Tim Jarvis | last post by:
Hi, I have an object that I am binding to a text box, this object exposes a boolean field, and I have implemented a format event handler and a parse event handler for the binding object, where I convert the bool value to some meaningful text. i.e. Binding b = new
3
3146
by: Kevin Swanson | last post by:
I'm writing what should be a very simple app against an Oracle database. The app has a number of user controls, any one of which is loaded into a main display page using the loadControl method, depending on which menu item a user selects. Each of these controls follows the same basic pattern: Get a dataset from the database and then display the results using basic databinding. Everything works fine except that I'll occaisionally get an...
3
1852
by: John Bailey | last post by:
When I first built a few web pages in ASP .Net 2.0, I thought it was great. The formview and detailview contorls would automatically layout the controls for you, the update methods were automatically generated, and the objectdatasource let you design against a business object through the gui. Now I am working on my first real web application on 2.0, and I find this automatic functionality doesn't do much beyond allowing me to generate...
6
2308
by: Nathan Sokalski | last post by:
I am using a DataSet as the DataSource of a DataList in my code. The SQL used to get the data from the database begins with: SELECT members.organization,artists.artist,artists.email,artists.website,members.email FROM members INNER JOIN artists ON members.memberid=artists.memberid WHERE Notice that both tables involved in the SELECT statement have a field named
7
2227
by: News | last post by:
Hello, I have to build a program with the future in mind and I need a bit of guidance from a guru or two. My program will start as a multi-user Windows Application built with VB.Net and using an Access 2002 database backend. The future will require that 1. The database be switched with minimal effort to SQL Server and 2. A Web Application be added to allow web access to reports generated from the database. At this time, there is no...
1
2585
by: David Veeneman | last post by:
Hi-- I'm trying to databind a business object to several controls using the DataBindings property of the controls, like this: textBoxStartDate.DataBindings.Add("Text", CurrentStep, "StartDate", true, DataSourceUpdateMode.OnValidation, String.Empty, "d"); Where CurrentStep is a business object of type ProjectStep, with several properties, such as the StartDate property shown above.
1
18835
by: Dave A | last post by:
Hi, I am struggling with two way databinding in WinForms and the DataGridView. I am binding to business object classes (rather than datatables). If I have a collection of these business objects that is the datasource of a DataBinding that is bound to a DataGridView (WinForms) then I was expecting that any change to the data would be reflected in the DataGridView (in other words I was expecting the 2 way databinding to actually...
7
1278
by: Ryan | last post by:
I'm in the process of learning more about building my ASP.NET website to use my SQL datastore and am a bit confused about how ADO.NET works with ASP.NET. This Microsoft article implies that using ADO.NET with ASP.NET applications is the way of the past because newer controls allow you to do all your data binding declaratively. http://msdn2.microsoft.com/en-us/library/ms178359(d=ide).aspx However, I haven't been able to get my application...
11
1801
by: =?Utf-8?B?R29rdWw=?= | last post by:
I am struck up with a problem and want anyone here to help me out. I am a beginner in .NET trying to learng DataBinding concepts. I have binded 4 text boxes with a dataset but when I say adapter.update it gives me 0 records updated! I am not getting any exceptions. Below is the complete code. Someone please help me out. using System; using System.Drawing; using System.Collections; using System.ComponentModel;
0
8459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8378
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8890
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8653
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2786
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.