473,792 Members | 3,005 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 1700
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
2351
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 data binding allows you to bind one data element to a control. In many situations you want to display...
6
13999
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 'KeyFlags(i) = rstFields("keyflag").value'
4
1981
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 reasonably simple stuff. I have a page where I'm showing product details, including any
2
1305
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 problem is that when i trap the format event of the data binding, i can get to the value before it...
19
2348
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 update several database fields for example.
0
1533
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 ComboBox using the combo's DataSource property, the combo box fills up with items from the array list....
2
4915
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 the datatable with a different record (new
0
2216
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 SQL Server 2005 on the back end and we're having a problem using ComboBoxes data bound to lookup...
14
14659
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
4027
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.. DatasetInit.Tables.Add(tblSliceInfo)
0
9670
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
9518
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,...
1
10159
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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
9033
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4111
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

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.