473,405 Members | 2,445 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,405 software developers and data experts.

DataBinding to Properties of a Collection

I have been messing around with data binding on Windows Forms, and I'm
having a problem with the Property of a Collection (myCollection.Count)
getting out of synch with label it's bound to.

(My real problem is binding to a Property of a Collection, and not just
the simpler case of the .Count property)

I'm using the following syntax to bind the object to the label.

1.) label1.DataBindings.Add("Text", myCollection.Count, "");

If I then add a new item to my collection through code, the label for
the Count does not get updated. I initially had this problem with a
simple object that has a TotalPrice property that is a calculated field
(based on Quantity * UnitPrice) , but I was able to add an event that
triggered the GUI to get the new calculated value.

public event EventHandler TotalPriceChanged;

public void OnTotalPriceChanged()
{
if (TotalPriceChanged!= null)
TotalPriceChanged(this, new EventArgs());
}

public int TotalPrice
{
get
{
return _totalPrice;
}
set
{
_totalPrice = value;
OnTotalPriceChanged();
}
}

In this case I had to use the following syntax to bind the TotalPrice,
where I specify the 3rd parameter as the property name, instead of
including it in the 2nd parameter as I did above.

2.) label1.DataBindings.Add("Text", myObj, "TotalPrice");

However, if I'm binding to a Property of a Collection using the Syntax
from #2, I get an System.ArgumentException 'Can not bind to property or
column Count on datasource.' What I think is going on here is that it's
trying to bind to the Currently Selected Item in the Collection, which
does not have a Count property.

Am I just missing something, or do Properties of Collections not have
any way to bind and keep their data synchronized between the Collection
object and the GUI?

TIA,
Charlie

Nov 16 '05 #1
3 3832
Charlie,

You are correct, when you do this:

label1.DataBindings.Add("Text", myCollection, "Count");

the binding code is trying to find Count property on element of the
collection.

What you could do is create a simple wrapper class for your collection with
Count property and CountChanged event and bound to it.

Alexander

"ch*****@noemail.com" <ch*************@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
I have been messing around with data binding on Windows Forms, and I'm
having a problem with the Property of a Collection (myCollection.Count)
getting out of synch with label it's bound to.

(My real problem is binding to a Property of a Collection, and not just
the simpler case of the .Count property)

I'm using the following syntax to bind the object to the label.

1.) label1.DataBindings.Add("Text", myCollection.Count, "");

If I then add a new item to my collection through code, the label for
the Count does not get updated. I initially had this problem with a
simple object that has a TotalPrice property that is a calculated field
(based on Quantity * UnitPrice) , but I was able to add an event that
triggered the GUI to get the new calculated value.

public event EventHandler TotalPriceChanged;

public void OnTotalPriceChanged()
{
if (TotalPriceChanged!= null)
TotalPriceChanged(this, new EventArgs());
}

public int TotalPrice
{
get
{
return _totalPrice;
}
set
{
_totalPrice = value;
OnTotalPriceChanged();
}
}

In this case I had to use the following syntax to bind the TotalPrice,
where I specify the 3rd parameter as the property name, instead of
including it in the 2nd parameter as I did above.

2.) label1.DataBindings.Add("Text", myObj, "TotalPrice");

However, if I'm binding to a Property of a Collection using the Syntax
from #2, I get an System.ArgumentException 'Can not bind to property or
column Count on datasource.' What I think is going on here is that it's
trying to bind to the Currently Selected Item in the Collection, which
does not have a Count property.

Am I just missing something, or do Properties of Collections not have
any way to bind and keep their data synchronized between the Collection
object and the GUI?

TIA,
Charlie

Nov 16 '05 #2
Charlie,

You are correct, when you do this:

label1.DataBindings.Add("Text", myCollection, "Count");

the binding code is trying to find Count property on element of the
collection.

What you could do is create a simple wrapper class for your collection with
Count property and CountChanged event and bind to it.

Alexander

"ch*****@noemail.com" <ch*************@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
I have been messing around with data binding on Windows Forms, and I'm
having a problem with the Property of a Collection (myCollection.Count)
getting out of synch with label it's bound to.

(My real problem is binding to a Property of a Collection, and not just
the simpler case of the .Count property)

I'm using the following syntax to bind the object to the label.

1.) label1.DataBindings.Add("Text", myCollection.Count, "");

If I then add a new item to my collection through code, the label for
the Count does not get updated. I initially had this problem with a
simple object that has a TotalPrice property that is a calculated field
(based on Quantity * UnitPrice) , but I was able to add an event that
triggered the GUI to get the new calculated value.

public event EventHandler TotalPriceChanged;

public void OnTotalPriceChanged()
{
if (TotalPriceChanged!= null)
TotalPriceChanged(this, new EventArgs());
}

public int TotalPrice
{
get
{
return _totalPrice;
}
set
{
_totalPrice = value;
OnTotalPriceChanged();
}
}

In this case I had to use the following syntax to bind the TotalPrice,
where I specify the 3rd parameter as the property name, instead of
including it in the 2nd parameter as I did above.

2.) label1.DataBindings.Add("Text", myObj, "TotalPrice");

However, if I'm binding to a Property of a Collection using the Syntax
from #2, I get an System.ArgumentException 'Can not bind to property or
column Count on datasource.' What I think is going on here is that it's
trying to bind to the Currently Selected Item in the Collection, which
does not have a Count property.

Am I just missing something, or do Properties of Collections not have
any way to bind and keep their data synchronized between the Collection
object and the GUI?

TIA,
Charlie


Nov 16 '05 #3
Alendaer,
Thanks for the information. I was hoping for a slightly cleaner
implementation than having to create another wrapper class for each
collection object in our business objects, but I guess that's what I'm
left with.

Charlie

Nov 16 '05 #4

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

Similar topics

0
by: Timothy White | last post by:
I have a Windows Form Which Displays one record at a time. The DataTable object which is bound to the Form Controls only contains the record that is being displayed. When I need to display a...
0
by: Wiktor Zychla | last post by:
Hello, I am thinking of a general solution to the specific issue and I hope someone has some more experience on that. Up to now I use the ListView as the primary control for collections of...
3
by: Nic | last post by:
Hey, Is it possible to do a databinding to a function, not to a property. Ex. Object : Invoices - Invoice Each Invoice has a collection of payments (an invoice can be paied in pieces) -...
1
by: Gary Shell | last post by:
I have a pair of combo boxes on a form. Both have their SelectedValue property bound to a column on a table called "Input_Output". One column is called "Class" and the second is called "SubClass"....
2
by: cbrown | last post by:
I am binding a custom collection to a combobox, which gives me no errors, but displays PintClub.Member for each entry. How can I set the displaymember and valuemember fields correctly. Do I...
1
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...
8
by: Dirk | last post by:
Hello, I have a problem to use databinding with my business layer classes. My data class does not have simple properties (string, int or datetime), instead, all my properties are objects of the...
2
by: cjard | last post by:
I ask, because I have a textbox bound to a bindingsource, which is bound to a datatable(row) The datatable is typed, and hence exports a Property called Column1 I added another property to the...
1
by: Craig Buchanan | last post by:
I have an class, named MessageTemplate, that has three properties: Name, Description, and Tokens. The Name and Description properties are strings. The Tokens property, however, is a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...

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.