473,387 Members | 1,512 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,387 software developers and data experts.

Binding a custom class to a DataGrid

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
Nov 16 '05 #1
6 7920
Hi,

one option is to create a Collection class based on CollectionBase - this
will bind to a datagrid and gives you lots of cool functionality like Add(),
Remove, indexing etc. etc. etc. Here's a walkthrough:
http://www.csharphelp.com/archives/archive256.html

Steve

"GingerNinja" <gr*******@hotmail.com> wrote in message
news:71**************************@posting.google.c om...
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

Nov 16 '05 #2
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
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?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
Well CollectionBase derives from IList so it should work straight off - I've
got loads of things derived from CollectionBase that display in a DataGrid -
you must be doing something wrong I think - here's a quick example I created
that should work for you:

http://www.willcockconsulting.com/Fi...inFormTest.zip

Steve

"Graham Innes" <gr*******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #5
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
"Graham Innes" <gr*******@hotmail.com> wrote in message
news:Ol*************@TK2MSFTNGP12.phx.gbl...
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?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #6
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #7

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

Similar topics

5
by: K | last post by:
I created a collection which is derived from ArrayList and implements IBindingList and ITypedList. Then I bound the DataSource of a data grid into the collection. It could show up the data but...
5
by: Dalibor Kusic | last post by:
The DataTable contains, for example, only one column of lets say Person class. Person class has three fields, and a constructor that takes a single string and parses it over those three fields. ...
0
by: berg | last post by:
All, I am binding an ArrayList full of custom objects to an DataGrid. The property in the custom class are all public. I define a DataGridTableStyle and set the MappingName to "ArrayList". I...
1
by: Conawapa11 | last post by:
I'm having trouble figuring this problem out and every example out there deals with simple objects within a custom collection. Take this example: public class ComplexClass { private int id;...
3
by: Just D | last post by:
Hi, If anybody needs to show some data retrieved from the database table, what method is more preferrable? 1. DataSet ds = ...; DataGrid.Data.Source.ds; DataGrid.Bind(); 2. Write a custom...
1
by: Demetri | last post by:
Someone posted the following back in June and I am now doing the same thing. I'll just paste what was asked and see if anyone can give us an answer: ...
0
by: Stephajn Craig | last post by:
I've built a customized collection class that has strongly typed objects in it. I have a collection class called LogEntryCollection with LogEntry objects in it. Each LogEntry object has certain...
1
by: viktor9990 | last post by:
I'm trying to bind a custom collection to an autogenerated datagrid without success. I'm getting this error: DataGrid with id 'DataGrid2' could not automatically generate any columns from the...
2
by: Merk | last post by:
I'm new to .NET (using 2.0) and was wondering how to go about binding a collection of custom classes to a DataGrid. The class exposes 5 read-only String properties. I would like for each of...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.