Using Objects as Data Sources

O

Otis Mukinfus

VS 2005 has the option of using objects as datasources.

I'm able to add a an object to my project as a datasource by using the
Data Source Configuration Wizard, but can't seem to make it do
anything. For example when I bind a list<> object to a DataGridView
control a BindingSource and BindingNavigator are created as I
expected, but when data is entered into the columns of the grid, they
disappear as soon as I move out of the cell.

I suspect this is because I have not prepared the object properly for
databinding, but I can't find anything that describes how objects that
are to be databound are to be constructed.

Can anyone tell me where I can find some information on designing
objects that will be used for databinding?

Thanks,

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 
W

William \(Bill\) Vaughn

Let me know if you find anything (let us all know). AFAICT this is very
lightly documented.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
G

Guest

This is one way that I know you can have design time support for objects.
There may be other ways to do this, but this works.

1. Create a class library project
2. Create your class with public properties
3. Create a separate Collections Class that inherits a bindinglist of class
you just made (Inherits BindingList(of employees)
4. use the imports statement at the top of your collections class and
import the System.ComponentModel name space. This is the first step for
design time databinding.

5. Under the Inherits statement you added, add 'Implements IComponent'

You will have to add finalizers and Sort methods of your own. You should
also add events for text public properties so that updates in your classes
will be reflected in your bound controls. There is some great source code for
these kinds of projects including how to add sorting logic.

1.1 and 2.0 frame work take a look at this.
http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx
 
O

Otis Mukinfus

Let me know if you find anything (let us all know). AFAICT this is very
lightly documented.

Boy you're sure right about that, Bill!

I tried making the object that was being put in the List<T> object use
the IEditableObject interface, but strangely that made it worse. now
it won't even show the data in the List<T> objects :0(

Joel Murach (Main Frame books writer) has a book out titled "ASP.NET
2.0 Upgrader's Guide" that devotes a chapter to "ObjectDataSources",
but the examples and the VS 2005 help seems to indicate what he is
writing about only applies to ASP.NET. All of his examples use an
ASP.NET control named "ObjectDataSource" to bind to objects that are
designed to be used by that control.

Some times I think MS has forgotten there are still folks developing
Windows applications who would benefit from some of the gadgets they
have for ASP.NET.

I'll let you know if I learn how to use the objects for binding in
windows apps.

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 
O

Otis Mukinfus

This is one way that I know you can have design time support for objects.
There may be other ways to do this, but this works.

1. Create a class library project
2. Create your class with public properties
3. Create a separate Collections Class that inherits a bindinglist of class
you just made (Inherits BindingList(of employees)
4. use the imports statement at the top of your collections class and
import the System.ComponentModel name space. This is the first step for
design time databinding.

5. Under the Inherits statement you added, add 'Implements IComponent'

You will have to add finalizers and Sort methods of your own. You should
also add events for text public properties so that updates in your classes
will be reflected in your bound controls. There is some great source code for
these kinds of projects including how to add sorting logic.

1.1 and 2.0 frame work take a look at this.
http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx

Thanks for the suggestion Steven. I did try using A class that
inherits from IBindingList<T>, but that didn't work either.

I'm still researching this. If I find the way or if someone tells me
the way, I'll post it here.


Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 
O

Otis Mukinfus

G

Guest

Show me an example of your code. I mostly use generic collection instead of
array list, data tables, and datasets and that seems to work well. One
question, are you making class libraries and compiling it to a dll or are you
just creating a class? You should bind to an iList interface that exposed the
collection that you created.
 
F

Frans Bouma [C# MVP]

Otis said:
Thanks for the suggestion Steven. I did try using A class that
inherits from IBindingList<T>, but that didn't work either.

I'm still researching this. If I find the way or if someone tells me
the way, I'll post it here.

Design time databinding doesn't support generics. So you always have
to use a class that's NOT generic.

I'm not sure what exactly you're trying to add. A general class that
implements IList can be used as a datasource but if you want to pop-up
a designer, the datasources won't help you as vs.net doesn't call
anything on those when dragged onto a form.

FB

--
 
O

Otis Mukinfus

Show me an example of your code. I mostly use generic collection instead of
array list, data tables, and datasets and that seems to work well. One
question, are you making class libraries and compiling it to a dll or are you
just creating a class? You should bind to an iList interface that exposed the
collection that you created.

It's in the attachment.
Thanks,


Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 
G

Guest

If you looked at the mentioned article it is kind of a hybrid. This uses a
generic collection that is a bindinglist(of t) that they hard code the class
into. It works quiet nicely in a design time environment. It uses generics to
achieve the desired results, but is not a pure generic base. I have used a
similar approach on many projects as I like to control as much of the code as
possible. In the mentioned article there are 2 projects, one which gives
design time support for objects that he attempts to make strongly typed, and
one for generics.
http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx
 
G

Guest

I looking at what you are trying to do, the only way I can see it work is by
making and compiling a class library that has you business object. Then you
add it to your toolbox and then you can bind to it. There is probably other
ways, but that is the one that I have gotten to work. I am sorry that I could
not have been of more assistance.
 
G

Guest

This may help, though I'm a bit late to this thread.
1. Define your class
2. Add BindingSource to Form
3. In IDE, set DataSource of the BindingSource to the Class
4. Set the DataSource of your DataGridView to the BindingSource
4. In code, at Form Load, create your class instances and populate a List(Of
yourclass) with the instances.
5. set the DataSource of the BindingSource to be the List(Of yourclass)

This works well for me, both for display and editing the data. If you
re-populate the List(Of ), you must do it something like:
' Clear it out
If bsSolutions.DataSource Is slns Then 'needed check for first time
thru
bsSolutions.Clear()
End If
'Whatever it takes to rebuild the List(Of )

bsSolutions.DataSource = slns
bsSolutions.ResetBindings(False) 'To make controls(s) update
content

Speaking of inadequate documentation ... Now try to find out how to do
ColumnClick sorts on the DataGridView.

The key to all of this is to do as much as possible (preferably all) thru
the BindingSource -- which makes Data Validation follow a different line than
you might initially think. (consider the BindingComplete event for data
validation).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top