Is it possible to bind to a collection of classes?

Moorzee

Well-known member
Joined
May 31, 2006
Messages
92
Location
England
Programming Experience
3-5
Is it possible to bind to a collection of objects?

I bad mouthed databinding a week or so ago and was told I was on the wrong track(cheers jmc!) Someone mentioned binding to own objects so thought I would give it a try.

I've created an address class. And populate a collection of addresses by adding each class created created at run time to a collection. My problem is, what is the datamember for my binding source?(I'm using bindingsource object so I can use bindingnavigator object!)

Any help will be greatly appreciated.

Cheers.
 
Last edited:
it would be the (class.)property name that you want to bind it to.
So if it's bound to the Address class, simply set the datamember to the property you want in that text box.

-tg
 
Sorry should've been clearer with my Q. I'm ok with binding a single object(textbox etc) to a field/property of an object. My problem is what do I set my bindingsource.datamember to be? I am setting the bindingsource.datasource to be my Addresses collection but what is the datamember?

E.g code:
VB.NET:
daAdapt.Fill(dsClientDetails, "ClientAddresses")
bsMsClientBindingSource.DataSource = Addresses
If dsClientDetails.Tables("ClientAddresses").Rows.Count > 0 Then
For Each row In dsClientDetails.Tables("ClientAddresses").Rows
Dim objAddress As New Address(row)
Addresses.Add(objAddress)
Next
 
 
End If
''''''*********************************************
bsMsClientBindingSource.DataMember = ????????????????????????WHAT HERE??????????
'*******Problem here.........
txtHouseNoName.DataBindings.Add("Text", bsMsClientBindingSource, "HouseNoName")
txtStreet.DataBindings.Add("Text", bsMsClientBindingSource, "Street")
txtArea.DataBindings.Add("Text", bsMsClientBindingSource, "Area")
txtTown.DataBindings.Add("Text", bsMsClientBindingSource, "Town")
txtCity.DataBindings.Add("Text", bsMsClientBindingSource, "City")
txtPostCode.DataBindings.Add("Text", bsMsClientBindingSource, "PostCode")
txtCounty.DataBindings.Add("Text", bsMsClientBindingSource, "County")
chkPostalAddress.DataBindings.Add("Checked", bsMsClientBindingSource, "IsPostalAddress")
rtbUnparsedAddress.DataBindings.Add("Text", bsMsClientBindingSource, "FullAddress")
BindingNavigator1.BindingSource = bsMsClientBindingSource
 
You don't set a DataMember. If you assign a DataSet to the DataSource then you assign the name of the DataTable to the DataMember. If you assign the DataTable itself to the DataSource then you don't assign a DataMember. Your collection is equivalent to a DataTable and is thus treated the same way. When you assign a BindingSource to the Datasource of a grid you don't assign anything to the DataMember of the grid, so this is just the same.
 
Why does my bindingnavigator do rock all on the movenext button event.

It did when I bound straight to a dataset and assigned the datatable to the datamember. The navigator recognises that there are 2 addresses in the collecton but does not display any data in the bound textboxes????

AAAAAAAAAAaaaaaaaaaaaarrrrrrrrrrrrgggggggghhhhhhhhhhhhhhhhh...:(
 
jmc - that makes no sence.... how in the world then, does one set the field/control relationship? The DataSource should be the DataTable, the DataMEmber should be the FieldName....

Moorzee - the navigator takes care of moving through the collection/dataset/datatable .... so far, this is the only example in MSDN I can find that shows how to do it:
http://msdn2.microsoft.com/en-us/library/ms158105.aspx

It uses "Customers.CompanyName" as the datamember.... so, your datamember should be your object's name.property name.

-tg
 
TG. Dat man jmac was correct. I simply set the datasource of my bindingsource object to be my collection(addresses) and then the bindingsource of my navigator to be my bindingsource obj. Then set my databindings on textboxes etc to be the fields of my bindingsource object. The navigator works sweet now.

The problem before?????

I had the line:
bsMsClientBindingSource.DataSource = Addresses

before the address objects were being created and added to the collection!
 
AAAAHH!! the light bulb comes on.... Okay, I get it know.... I had my object cornfused.... I see now what y'all are talking about.

-tg
 
WOW....... I have just read that article Eric and am buzzing my titties off.

Has anyone around here used the drag and drop method of creating an edit form using objects as the datasource? I am just having a little crack at it and am wondering if it is worth continuing with? If anyone has already tried and come up with compelling reasons to leave well enough alone then I'll not bother continuing..

Also if I have an integer property in my object that I want to use as a valuemember ina combo how would I get the displaymember from a seperate location? Does that make any sense? What I am trying to ask is if I drag and drop the property onto my editform as a combo is that not my binding? How can I also bind to a dataset to populate with displaymember values?
 
Just a quick update in case anybody is remotely interested. Have been messing around and so far am well impressed with this method of binding. The ease of dragging dropping textboxes/combos/anything, already linked to your custom objects as a datasource is unreal. :cool: This should be plastered all over the box for dotnet. Forget binding to datasets, so old skool;) . This is the gear!:) (Until I get stuck that is!!:D )
 
Back
Top