473,407 Members | 2,312 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,407 software developers and data experts.

Show collection of objects in grid

I wish to show a collection of objects in the DataGridView control. This is
working fairly well except in one particular case. If my collection starts
empty when I assign it to the grid then when an item is added, the item
appears but every cell in the grid is blank. I am assigning the collection
to the grid like this:

MyGrid.DataSource = MyCollection

This works great if the collection contains 1 or more items but in the case
that the user is creating a new collection from scratch then it does not
work. I can see what is happening, the grid doesn't know which columns to
bind to which properties of my object if the collection has not objects. So
far I've managed to get around this by rebinding my collection to the grid
when the first item is added but this seems like a hack. Anyone have a
better solution?

BTW, I create the columns in the grid at design time and have set
AutoGenerateColumns to false.

Thanks,
Michael
Nov 6 '08 #1
4 1638
What is MyCollection?

WinForms uses various approaches for obtaining metadata (i.e. columns):

[don't worry about these first two; you don't need to know them...]
* if the source implements IListSource, the IList is obtained
* if the source implements ITypedList, this is queried for metadata

then:
* if the source implements IList and has a typed (non-object) indexer
"SomeType this[int]", then the "SomeType" is inspected
* if the source implements IList and is non-empty, the first item is
obtained and inspected
* if the source doesn't implement IList, it might (depending on the
control) get used as a single row
* otherwise you get nada

If you use List<T>, Collection<T>, etc you automatically get the typed
indexer, so this is the simplest fix. Don't use ArrayList.

Marc
Nov 6 '08 #2
"Marc Gravell" <ma**********@gmail.comwrote in message
news:OY*************@TK2MSFTNGP05.phx.gbl...
What is MyCollection?
It implements IBindingList but it is my collection so I can do as I need.
WinForms uses various approaches for obtaining metadata (i.e. columns):

[don't worry about these first two; you don't need to know them...]
* if the source implements IListSource, the IList is obtained
* if the source implements ITypedList, this is queried for metadata

then:
* if the source implements IList and has a typed (non-object) indexer
"SomeType this[int]", then the "SomeType" is inspected
* if the source implements IList and is non-empty, the first item is
obtained and inspected
* if the source doesn't implement IList, it might (depending on the
control) get used as a single row
* otherwise you get nada

If you use List<T>, Collection<T>, etc you automatically get the typed
indexer, so this is the simplest fix. Don't use ArrayList.
I'll give it a go, it can't hurt to implement another interface :-)

Thanks for your help,
Michael
Nov 9 '08 #3
Indeed. In that case, it should just need IList plus a typed indexer.
Nov 9 '08 #4
"Marc Gravell" <ma**********@gmail.comwrote in message
news:83**********************************@a17g2000 prm.googlegroups.com...
Indeed. In that case, it should just need IList plus a typed indexer.
Just IList on its own is no good because you can't raise an event to tell
the grid that items have been added/removed/changed. IBindingList has such
an event (I can't remember what it is called but it's only got 1 event).

Michael
Nov 10 '08 #5

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

Similar topics

2
by: Marc Scheuner [MVP ADSI] | last post by:
Folks, Has anyone done this before? (I'm sure some of you guys have) I'd like to make sure our business objects and collections thereof are created in such a way that we can connect them to a...
1
by: | last post by:
Hi I'm sorry for my english... I have a clas that contains a variable number of data stored in a dictionary. The key of the dictionary is a string that defines the name of the field. ( varA ...
1
by: Josema | last post by:
Hi to all, i have a class (of type collectionbase) that creates a collection of objects for instance persons. For other hand i have a Datagrid whose Datasource its the collection wich i...
7
by: Pete Davis | last post by:
A different question this time. I have a DataGrid bound to a collection. Is there any way for me to allow sorting? The DataGrid.AllowSorting=true doesn't work, but that's probably because it can't...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
11
by: Michael Kellogg | last post by:
I have a collection of custom objects that I am displaying in a Windows Forms Datagrid. To display it, I take the collection and build a DataTable object on the fly, then set the grid's DataSource...
0
by: Charles Krug | last post by:
List, I'd like to do the following with Tkinter's Frame() object: 1. Display a collection of pack()-able objects. Easy. Done. I hold the objects in a dictionary, mostly so that the owning...
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...
1
by: asharda | last post by:
I have a custom property grid. I am using custom property grid as I do not want the error messages that the propertygrid shows when abphabets are entered in interger fields. The custom property...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
0
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...
0
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...

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.