473,549 Members | 2,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

data binding a simple value

Hi to all,

I have an object that has a property numberOfFilesDe leted which is an
integer. I want to bind a label to it so that when the value changes, the
label gets updated on the screen.

I have used the following code to bind the text property of the label to the
object property.

private void WinForm_Load(ob ject sender, System.EventArg s e)
{
label1.DataBind ings.Add(new Binding("Text", directorySweepO bj,
"numberOfFilesD eleted"));
}
When I do the following :
private void button1_Click(o bject sender, System.EventArg s e)
{
directorySweepO bj.numberOfFile sDeleted =
directorySweepO bj.numberOfFile sDeleted + 1
}

I would expect the label to be updated, but it is not.

What am I doing wrong?

Sham.
Jan 31 '06 #1
3 1689
Your object needs to tell the UI that it has changed; this can be done in
two ways:

1.1 style: create a public event on your class called {propertyname}C hanged,
and call this event when your values change

2 style: implement the INotifyProperty Changed interface, and call this event
(specifying the name of the property) when the values change

Following is an example of the latter:

public class MyClass : INotifyProperty Changed {
private int someField;
public int SomeProperty {
get {
return someField;
}
set {
if (someField != value) {
someField = value;
OnPropertyChang ed("SomePropert y"); // oh for missing the
nameof(SomeProp erty) {or nameof(method)? } function...
}
}
}
protected void OnPropertyChang ed(string propertyName) {
if (PropertyChange d != null)
PropertyChanged (this, new
PropertyChanged EventArgs(prope rtyName));
}
public event PropertyChanged EventHandler PropertyChanged ;
}
Jan 31 '06 #2
Hi Marc,

Thanks for the reply.

I just started looking at C# last month in my spare time. So it looks like I
completely misunderstood databinding. I thought that was the whole point of
it (i.e. some sort of subject-observer pattern), where by specifying the
binding would handle the updating, registering of observer, notification
etc.

The code I have seen in the past with databinding was always with dataSets
and not with object properties.

So, I still need the data binding code -> label1.DataBind ings.Add(new
Binding("Text", directorySweepO bj,
"numberOfFilesD eleted"));

and I then introduce the notification code in my object? Is that correct?

Thanks once again.
Sham.

"Marc Gravell" <mg******@rm.co m> wrote in message
news:uf******** ******@TK2MSFTN GP09.phx.gbl...
Your object needs to tell the UI that it has changed; this can be done in
two ways:

1.1 style: create a public event on your class called
{propertyname}C hanged, and call this event when your values change

2 style: implement the INotifyProperty Changed interface, and call this
event (specifying the name of the property) when the values change

Following is an example of the latter:

public class MyClass : INotifyProperty Changed {
private int someField;
public int SomeProperty {
get {
return someField;
}
set {
if (someField != value) {
someField = value;
OnPropertyChang ed("SomePropert y"); // oh for missing
the nameof(SomeProp erty) {or nameof(method)? } function...
}
}
}
protected void OnPropertyChang ed(string propertyName) {
if (PropertyChange d != null)
PropertyChanged (this, new
PropertyChanged EventArgs(prope rtyName));
}
public event PropertyChanged EventHandler PropertyChanged ;
}

Jan 31 '06 #3
Correct;

IIRC DataSets already include this functionality, which is why you haven't
seen it so far. If you think about it. it makes sense: you don't want the UI
constantly probing your objects to see which properties have changed.

For ref, if you bing to a BindingSource instead (which sits between the UI
and the objects) you can call ResetBindings() on the source to force a
re-read from the object (e.g. when you think it has changed), but IMO it is
better to use notification - this avoids a lot of problems.

Marc
Jan 31 '06 #4

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

Similar topics

0
2328
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 visual representation of data. Two types of data binding are available for Windows Forms: Simple Data Binding and Complex Data Binding. Simple...
6
13988
by: Andy Barber | last post by:
Hi, I'm trying to write an app that reads data from a table into a string variable for later use in my program. Below is a snippet of the code I'm using, which compiles ok, but at runtime I get and error 'Object reference not set to an instance of an object.' as soon as I try to access the data in the fields, I.e. at the line that reads...
4
1967
by: Alan Silver | last post by:
Hello, I'm trying to use an ArrayList to do data binding, but am getting an error I don't understand. I posted this in another thread, but that was all confused with various other problems, which could be why no-one answered it!! I've now narrowed the problem down, so am starting a new, more specific, thread. Sorry it's a bit long, but it's...
2
1296
by: Tubs | last post by:
I am trying to use data binding to avoid having to write event handlers everywhere for my data transfer from controls to data source and all is working well except, i have a condition i would like to place on the value before i let it go through. I have built an expression evaluator to determine when something is valid versus invalid. My...
19
2322
by: Simon Verona | last post by:
I'm not sure if I'm going down the correct route... I have a class which exposes a number of properties of an object (in this case the object represents a customer). Can I then use this object to databind to text boxes etc? I can't use a dataset as the object has loads of derived logic, for example updating one property may actually...
0
1512
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. There are cases where properties of one object is dependant on a property of another object. For a common example, when you bind an ArrayList to a...
2
4891
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 TextBoxes) works fine the first time round. I can see, edit and update the details from my form, then close the form. I then like to select and refill...
0
2201
by: mjsterz | last post by:
I've been working with VB .NET for less than a year and this is the first time I've posted on one of these groups, so let me apologize beforehand if I'm being unclear, not posting my issue correctly, posting to the wrong forum, or committing some other sort of faux pas. My team is developing a Windows Forms application using VS 2005 with...
14
14615
by: Rolf Welskes | last post by:
Hello, I have an ObjectDataSource which has as business-object a simple array of strings. No problem. I have an own (custom) control to which I give the DataSourceId and in the custom-control so I get the ObjectDataSource. No problem ..... ObjectDataSource src = .... //is ok i have it
9
4000
by: Anil Gupte | last post by:
After reading a tutorial and fiddling, I finally got this to work. I can now put two tables created with a DataTable class into a DataRelation. Phew! And it works! Dim tblSliceInfo As New DataTable("SliceInfo") Dim tblSliceRatings As New DataTable("SliceRatings") '.... All the adding datacolumns, datarows, etc. goes here.. ...
0
7524
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...
0
7720
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. ...
0
7960
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...
0
7812
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...
1
5372
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5089
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1944
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
0
766
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...

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.