473,785 Members | 2,919 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons;

public class Myclass: IList

And I know I must now implement certain methods like:
System.Collecti ons.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 7949
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

"GingerNinj a" <gr*******@hotm ail.com> wrote in message
news:71******** *************** ***@posting.goo gle.com...
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.Collecti ons;

public class Myclass: IList

And I know I must now implement certain methods like:
System.Collecti ons.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*******@hotm ail.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.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*******@hotm ail.com> wrote in message
news:Ol******** *****@TK2MSFTNG P12.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
2535
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 the column heading is not in the right order, for eg. I want "ID" column to be the first column but it showed as the second column. Another problem is that anyway i can leave a blank row in the grid to add a new row automatically if the user...
5
11192
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. Now, how do I modify the Person class (which interfaces to implement) so I can bind my DataTable to a DataGrid and then add, modify and delete records through it?
0
1695
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 then add a DataGridTextBoxColumn for each of the properties in my custom class which define a MappingName --> property name in class as well as header text. When I run the form, I see correct number of rows displayed, but none of the columns...
1
2228
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; private InnerClass anotherOne; public int ID
3
1658
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 object, fill it with the DataSet data and ÅÒÕÔ try to bind
1
3157
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: --------------------------------------------------------------------------------- I have a custom collection of objects, each of which includes a child object called MyUserOpener. In declarative binding, I can bind this property to a label in a template control with the following syntax: ...
0
1293
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 properties similar to the one below: Public Property LogDate() As Date Get Return _LogDate End Get
1
2707
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 selected data source. Could you help me to find a solution? What is the problem in my code? Thanks. Here is my code (NewsContent.aspx): <form id="Form1" method="post" runat="server">
2
2218
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 these properties to appear in a column in the DataGrid. The end result would be that when the collection has 35 objects in it, the DataGrid would have 35 rows and 5 columns (after being bound to the collection).
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.