How best to use DGV to display Collection

bdam

Member
Joined
Jan 13, 2010
Messages
6
Programming Experience
5-10
I'm looking for some guidance on how best to accomplish something. I am using an API where I'm constantly receiving collections of custom objects that I need to represent in some sort of grid. Almost universally I need to take the data in one of the columns and run another API call using it. For instance, the objects in the collection from the first call will have an ID string that I use to make another call to get the name.

I have tried two approaches to using a DataGridView:
I've simply set the collection as the data source. If I remember correctly, that method wouldn't allow me to sort.
I then created routines that would create a temporary datatable, create the columns I need, loop through each item in the collection adding it to the table, and then setting the datasource to equal the datatable.

This second method works for the most part but there are certain things I keep banging my head against. I am using the SelectionChanged event to load further details about the selected row into a panel below the DGV. When the datasource is set/changed this routine gets called a whole bunch of times and will fail because the various objects (ex. CurrentRow) I'm calling are null. When the DGV is sorted it gets called again and will fail for the same reasons. I can come up with kludges to avoid both but they are just that, kludges.

Is there a better way to do this that avoids these problems? Would binding help? If not, is there a way to evaluate if the DGV is loading or sorting so I can avoid running the SelectionChanged code?

Thanks,
Bryan
 
Back
Top