Groups | Blog | Home
all groups > dotnet windows forms databinding > december 2005 >

dotnet windows forms databinding : Object Databinding in .NET 2.0


David Veeneman
12/28/2005 3:41:21 PM
I have been experimenting with object databinding in .NET 2.0, using Mike
Weinhardt's great MSDN columns as a guide:

Part 1:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms11162004.asp
Part 2:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms02182005.asp
Part 3:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms02152005.asp

I have built a run-of the-mill Customer object and a CustomerList
collection, which is derived from BindableList<T> and which implements
searching and sorting per Mike's columns. Now I want to bind the
CustomerList to a DataGridView at design-time.

Here's what I'm not sure about: In .NET 1.x, I would have needed to
implement IComponent on the CustomerList collection in order to bind the
collection at design time. But it looks like I don't need to implement
IComponent in .Net 2.0. I can identify CustomerList as a Data Source, and
then bind the DataGridView to CustomerList. The Designer sets up a
BindingSource object between the two, and we're off and running. It all
seems to work fine.

Here's my question: Am I doing this the 'right' way? I know I could let the
Designer create the CustomerList collection for me--I rolled my own so I
could add sorting and searching capabilities to the collection. But would
this design-time approach of identifying the collection as a Data Source be
considered a best practice, or is there another, better way to enable
design-time binding to a collection?

Thanks in advance for your help!

David Veeneman
Foresight Systems

David Veeneman
12/28/2005 4:33:54 PM
Thanks!

Joanna Carter [TeamB]
12/28/2005 9:55:54 PM
"David Veeneman" <davidv@nospam.com> a écrit dans le message de news:
Osnlzd$CGHA.2664@TK2MSFTNGP15.phx.gbl...

| Here's my question: Am I doing this the 'right' way? I know I could let
the
| Designer create the CustomerList collection for me--I rolled my own so I
| could add sorting and searching capabilities to the collection. But would
| this design-time approach of identifying the collection as a Data Source
be
| considered a best practice, or is there another, better way to enable
| design-time binding to a collection?

This method seems to work very well and AFAICT appears to be the best way to
do things.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Bart Mermuys
12/29/2005 12:31:57 AM
Hi,

[quoted text, click to view]

It's a little confusing, but if you drag a Data Source (custom collection)
onto the Form, then you should notice there is no instance of the custom
collection anywhere, infact if CustomerList doesn't implement ITypedList
(which isn't a requirement) then BindingSource.DataSource will point to
typeof(Customer) not even typeof(CustomerList), but even if it would it's
still not an instance, and that's the problem.

If the DataSource of a BindingSource is typeof(Customer), it will create a
BindingList<Customer>, even if the DataSource is typeof(CustomerList) (which
doesn't implement ITypedList) it will still create a BindingList<Customer>
and not a CustomerList as you might imagine. The main goal of a
BindingSource when it's bound to a type is to have a "temporarily"
collection whose items show the same properties so you can setup the
Controls at design, but the same happens at runtime.

So, yes it's ok to drag a Data Source (custom collection) to the Form, BUT
it's not enough, you need to add an instance too, and there are two options:

1) You add a field to your Form from code and instantiate it from code:

public class MyForm ...
{
// add field
private CustomerList cl = new CustomerList();

private void Form_Load( ... )
{
CustomerListBindingSource.DataSource = cl;
}
}

2) You can implement IComponent and then after dragging the Data Source
onto the Form you also drag the CustomerList from the toolbox on the Form to
have an instance and then change the CustomerListBindingSource.DataSource to
that instance with the designer.

So i repeat, the dragging of an object Data Source should only be used as a
model to setup the Controls, it still very wise to actually create an
instance (from code or with a component) and then change
BindingSource.DataSource to that instance.

HTH,
Greetings

[quoted text, click to view]

AddThis Social Bookmark Button