Groups | Blog | Home
all groups > dotnet general > september 2006 >

dotnet general : Putting list of objects into a datagridview


Tolgan
9/7/2006 2:49:36 PM
I have an object class "Person" say, with various public properties
including say "Name" and "Salary"

I have several person objects that I have created by some method outside the
scope of this discussion.

I want to setup a datagridview with each row corresponding to one person
object, and two columns being "Name" and "Salary". I want the user to be
able to click on the column heading to sort rows based on values in that
column. Users are not permitted to add to change rows, though can select
rows as operands to other operations.


This feels like it should be straightforward by setting up a collection of
persons as a datasource property either of the dataviewcontrol itself, or of
a bindingsource (which is then itself attached to the datagridview). However
whenever I go this route I seem to lose the (user) ability to sort on column
values.

Or, I can just add rows of {person.name, person.salary} pairs directly to
the datagridview but this is not very satisfactory as when the user selects
I want to get back the person object, not just the name and salary values.

What am I missing?


Joanna Carter [TeamB]
9/7/2006 11:58:41 PM
"Tolgan" <tolgan@NOTTHISntlworld.com> a écrit dans le message de news:
1157637012.13467.0@iris.uk.clara.net...

| What am I missing?

Create an instance of the BindingList<T> class, passing your list to the
constructor, then set the DataGridView to that as DataSource. You may also
need to write a Comparer<T> derived class to implement IComparer<T> to
achieve sorting.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Tolgan
9/8/2006 6:15:41 PM
Gosh - a bit more programming than I expected to have to do given how nearly
the datagridview does it on its own without any datasource! But I've tried
your suggestion, certainly works and haven't come up with anything better,
so many thanks!

Chris (tolgan)

[quoted text, click to view]

AddThis Social Bookmark Button