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

Grid DataItem and Anonymous Types

7 Answers 509 Views
Grid
This is a migrated thread and some comments may be shown as answers.
wdudek
Top achievements
Rank 1
wdudek asked on 19 Jun 2008, 05:11 PM
I have a grid that I am populating using LINQ and an anonymous type, the grid consists of 1 column with a hyperlink and can be populated by one of 2 data sources. Depending on the datasource I need to change the hyperlink and query parameters. I am trying to access the values for the parameters from the e.Item.DataItem, but being that it is an anonymous type, I'm not sure how to get at the values even though I can see them in the debugger. Anyone else come across this?

7 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 20 Jun 2008, 03:00 PM
Hello William,

Can you try casting your e.Item to a GridDataItem first, and then reference the value for a specific column using the column's unique name. The Text property of the TableCell thus referenced should be holding a string representation of your value:

GridDataItem item = (GridDataItem)e.Item; 
string cellValue = item["ColumnUniqueName"].Text; 


All the best,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Shinu
Top achievements
Rank 2
answered on 23 Jun 2008, 06:41 AM
Hello William,

You can also go through the following help article.
Accessing cells and rows

Thanks
Shinu.
0
wdudek
Top achievements
Rank 1
answered on 23 Jun 2008, 03:44 PM
Hi, 
    My problem isn't accessing the data in the cells, I can get at the hyperlink column and at the data item that would be supplying it. My problem was that in the code behind I was trying to extract values from the data item, but because it is an anonymous type I don't know what to cast it to to get the data out of it. It's more related to LINQ than to Telerik, but I was wondering if anyone here had solved the same problem already?
0
Accepted
Nikolay Rusev
Telerik team
answered on 25 Jun 2008, 08:30 AM
Hello wdudek,

Thank you for contacting us.

If I understand you correctly your scenario is similar to the following:

RadGrid bound to collection of
select new  
{  
 //property  
 Name = "some value",  
 //property  
 Thread = "some value" 
}; 

and you are trying to extract the data of Name property on some event of the grid ex. ItemDataBound.
Here you have the DataItem:
var _item = e.Item.DataItem; 

You have two options:
Type _t = (_item.GetType().GetProperty("Name").PropertyType);  
object name = Convert.ChangeType(DataBinder.Eval(_item, "Name"), _t);  
 
OR  
 
var name = DataBinder.Eval(_item, "Name"); 

Unfortunately as you can see there is no way to cast to the type of that property.

Greetings,
Nikolay
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
wdudek
Top achievements
Rank 1
answered on 25 Jun 2008, 02:08 PM
That is exactly my situation. Thanks!! My work around was to create a struct and populate the struct with the results from LINQ instead of using the anonymous type, but this seemed kind of excessive to populate the simple data I was collecting. For the page I am working on your example will make my code allot cleaner.

Thanks

Bill
0
Juan Angel
Top achievements
Rank 1
Veteran
answered on 16 Apr 2009, 09:54 AM
My problem is very similiar:

I Have an anonymous type in Grid Datasource, like this
var expedientes = (from exp in tuc.EXPEDIENTE 
           select new { exp.ID_EXPEDIENTE, exp.DSC_EXPEDIENTE, 
, ENTRADAs = (from et in tuc.ENTRADA_TUC where et.ID_EXPEDIENTE == exp.ID_EXPEDIENTE select et.ID_ENTRADA)                                                 
                                             } 
                                            ).ToList(); 

I Have 2 entities (tables) EXPEDIENTE and ENTRADA. I obtain objects of EXPEDIENTE and related objects of ENTRADA. The propety ENTRADAs is a collection of ENTRADA.

I use this code:
Type _t = e.Item.DataItem.GetType().GetProperty("ENTRADA").PropertyType;   
object obj = Convert.ChangeType(e.Item.DataItem, _t); 

the first line it's ok, but Convert.ChangeType throw an System.InvalidCastException: Object must impletent IConvertible.

I need access to property ENTRADA and its elements.

Thanks.
0
p
Top achievements
Rank 1
answered on 03 Feb 2010, 08:10 PM

Try
DataBinder.Eval(e.Item.DataItem, "PropertyName") 


It worked for me.

Source:
http://stackoverflow.com/questions/1470907/access-columns-in-itemdatabound-event-when-the-datasource-is-linq


Tags
Grid
Asked by
wdudek
Top achievements
Rank 1
Answers by
Veli
Telerik team
Shinu
Top achievements
Rank 2
wdudek
Top achievements
Rank 1
Nikolay Rusev
Telerik team
Juan Angel
Top achievements
Rank 1
Veteran
p
Top achievements
Rank 1
Share this question
or