This is a migrated thread and some comments may be shown as answers.

Retrieving Object Definitions after they have been placed in a Grid

4 Answers 23 Views
GridView
This is a migrated thread and some comments may be shown as answers.
James Grise
Top achievements
Rank 1
James Grise asked on 13 Mar 2010, 02:59 AM
How do I get the data that I have placed into a gridView back out in the same fashion that I put it in.  I'm having a tough time with creating interactions with the grid view because I have to use some very complicated reflection techniques to retrieve the data objects based on their selection. 

In short, after I have said myGridView.itemsSource = myOriginalDataCollection<myOriginalDataObject>

How do I get from myGridView.ItemsSelected to myOriginalDataObject.allMyUntouchableAttributes?

Thanks
James Grise
Scenario VPD
SoftwareEngineer

4 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 15 Mar 2010, 07:34 AM
Hi James Grise,

RadGridView has a property called SelectedItems which contain all data item that are selected. Cou can use this property to expract the items that you need.


Best wishes,
Milan
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
James Grise
Top achievements
Rank 1
answered on 15 Mar 2010, 08:59 PM
This isn't exactly true.  If I just want to redisplay the info that I have shown in a grid the myGrid.SelectedItems works fine so if I want to say myList.ItemsSource = myGrid.SelectedItems this might work.  The difficulty for me comes in understanding the process of retrieving the data that is displayed.  Say I have a Collection of type User, the User class has all the usual user stuff, name, id, address, phone, etc.  What if email is actually a collection of email addresses to be able to track multiple email addresses.  If I want to retrieve that list of addresses, I cant just say myGrid.selectedItems.emailAddresses, I will get an invalid cast exception because the objects returned by the grid list are not of type User.  Instead I need to be able write something that casts the User data back into the User class so that I can once again use the class attributes individually.  Something like foreach(User myUser in ((User)myGrid.SelectedItems){ myListBox.ItemsSource = emailAddresses);  So rather than being concerned with the display of an object which is really only a list of names, I get achievable access to the class objects that are represented within the display based on the selection.  This will allow me to leverage the sorting and filtering functions that the Grid has built in instead of having to rewrite new collections each time a selection is made.

-James
0
Accepted
Milan
Telerik team
answered on 15 Mar 2010, 10:07 PM
Hi James Grise,

Well, if you would like to access specific properties you have to cast the objects in SelectedItems to their respective type. You can easily do that by using the OfType extension method:

//using System.Linq;
var myTypeList = this.myGrid.SelectedItems.OfType<myType>();

Regards,
Milan
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
James Grise
Top achievements
Rank 1
answered on 15 Mar 2010, 10:49 PM
Thanks Milan, I was trying to avoid creating another collection where one already exists in the same scope but I guess this can't be helped.  Thanks
Tags
GridView
Asked by
James Grise
Top achievements
Rank 1
Answers by
Milan
Telerik team
James Grise
Top achievements
Rank 1
Share this question
or