I tend to use LINQ for just about everything I can. Many times this involves populating ASP.NET GridViews from code with anonymous types from LINQ. For example:

The issue that I came across is that when I need to access data from that query during an OnRowDataBound event. When trying to access the item using the following code I would get error about casting anonymous types to DataRowView:

Using the suggestion from this Stack Overflow answer I was able to retrieve the value by reflecting the anonymous type:

I’m sure there is a more efficient way to do this, however this works for me. Hopefully you find it useful too.

Pin It on Pinterest

Share This
%d bloggers like this: