How can I bind a Windows Forms Datagrid to a collection?

M

Michael Kellogg

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 to the DataTable. The
code for this is lengthy and the grid is obviously not editable. I've read
it's possible to connect my collection directly, and have it be editable,
too. Can anyone point me to a resource that can walk me through this?

TIA
 
K

Ken Tucker [MVP]

Hi,

If you set the datagrids datasource to the collection any properties
in your custom class will be displayed in the datagrid. If you are adding a
tablestyle to the datagrid use collection as the mapping name.

Ken
--------------------
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 to the DataTable. The
code for this is lengthy and the grid is obviously not editable. I've read
it's possible to connect my collection directly, and have it be editable,
too. Can anyone point me to a resource that can walk me through this?

TIA
 
M

Michael Kellogg

Ken Tucker said:
Hi,

If you set the datagrids datasource to the collection any
properties
in your custom class will be displayed in the datagrid. If you are
adding a tablestyle to the datagrid use collection as the mapping
name.

I've tried a few times to just set the datagrid's datasource to my
collection but all I get is a blank datagrid. I know the collection is
full of data; what am I missing?

mk
 
B

bill

Sorry, I was thinking of datasets not collections.

Does your custom object class have a Get property (such as 'Name') to be
displayed in the datagrid?
 
M

Michael Kellogg

bill said:
Sorry, I was thinking of datasets not collections.

Does your custom object class have a Get property (such as 'Name') to be
displayed in the datagrid?

No, I didn't get that complicated with it. It's little more than about 20
"fields". Do I need to set a Default Get property or something?
 
B

bill

That is how I did it. If your object has Get properties, the datagrid will
display them. There should be one property for each field to be displayed
in the datagrid.

For example, if the widget class has a public WidgetName property which
returns the widget name, there will be a WidgetName column in the datagrid.
It will display the name property for each widget entered into the Widget
collection which is the data source for the datagrid. If there is no Get
property function, the datagrid has nothing to display.
 
M

Michael Kellogg

bill said:
That is how I did it. If your object has Get properties, the datagrid
will display them. There should be one property for each field to be
displayed in the datagrid.

For example, if the widget class has a public WidgetName property
which returns the widget name, there will be a WidgetName column in
the datagrid. It will display the name property for each widget
entered into the Widget collection which is the data source for the
datagrid. If there is no Get property function, the datagrid has
nothing to display.

Well I figured that since all my member variables were Public, that would
take care of that. I will experiment with a GET and see what happens.

Thanks!
 
B

bill

I tried that too. The data grid didn't see the member variables, I guess.
It needs Get property procedures. (At least that's how it worked for me)
Good luck.
-Bill
 

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