Binding a custom class to a DataGrid

G

GingerNinja

Hi Everybody, its about my 4th day on C# and all seems to be going
smoothly however I've started to get into Datagrids and in particular
binding data sources to them. In the documentation it says I can bind
all the usual Datasets etc but it also says I can bind the following:

Any component that implements the IListSource interface
Any component that implements the IList interface

Now I have a class that holds lots of repeating data, and to save me
having to create datasets and looping through adding the data manually
then binding to the datagrid. It would be great to implement the IList
or IListSource interface, trouble is I'm not really sure how to get
started.... I kind of figures it goes:

using System.Collections;

public class Myclass: IList

And I know I must now implement certain methods like:
System.Collections.ICollection.CopyTo(System.Array, int)

But I have no idea what to do with these methods, you could say I'm a
bit clueless with this and I could do with a push in the right
direction.

I really would appreciate some guidance on this as my searches in
various help files and websites hasnt helped.

Thanks
 
G

Graham Innes

Steve,

Thanks for the response and the example, it was very informative. I've
followed the example and while it seems to work ok, it doesnt bind to a
datagrid. I guess thats because its not Implementing the IList interface
but rather the CollectionBase which a Datagrid doesnt support as a
datasource. Right?

So I would follow the example adapting it but instead of using the
CollectionBase I use the IList interface?

Regards
Graham
 
G

Graham Innes

Actually forget my Post above it DOES bind to a datagrid I'd just got my
syntax wrong, FYI I've also written a class Implementnig the IList
interface which also binds to a class.... Now decision, decision which
one do I use, the one in that article or my IList interface?

Opinions?
 
S

Steve Willcock

As I pointed out in my previous post (I didn't see this post from you :) ),
CollectionBase derives from IList anyway, not sure if that influences your
decision?

Steve
 
G

Graham Innes

Steve,

DOH!! Of course it does, and because of that I dont need to Implement
IList because it already is implemented in CollectionBase.

It all seems very simple now, much appreciate for your help.

Regards
Graham
 

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