LinqDataSource and GridView.RowDataBound

D

david

I've bound my GridView to a LinqDataSource control, but now I want to do
some work in the RowDataBound event.

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
var item = e.Row.DataItem;

But now what do I do. When using ObjectDataSource and DataSets, I could
cast that item to a DataRowView and access the properties from there.
But here it seems I have an anonymous type.

As far as I can see, my only options are Reflection or DataBinder.Eval
with this object. Is there any way to coerce the LinqDataSource into
giving me a non-anonymous type?

Or is there a better workaround to this?

TIA
David
 
M

Md. Masudur Rahman

Hi,

try casting your dbcontexts class type...
suppose i have a table category in my db....
and in dbml i have a class called category...
here is a example code...
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Category c = (e.Row.DataItem as Category);
}
}

--
Regards

Md. Masudur Rahman
===============

Software Engineer
Kaz Software Ltd.

http://www.shatkotha.com
http://www.munna.shatkotha.com
http://www.munna.shatkotha.com/blog
 
D

david

Md. Masudur Rahman said:
try casting your dbcontexts class type...
suppose i have a table category in my db....
and in dbml i have a class called category...
here is a example code...
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Category c = (e.Row.DataItem as Category);


thanks, I appreciate it, but I do realize that if I don't have an
anonymous type I can cast the DataItem to the type it is.

The problem occurs when I *do* have an anonymous type: in this
particular case because I've set the Select property on the DataSource
but the same thing happens if I do something like add a join to the query.

In those cases, the above is just going to return null, since
e.Row.DataItem is not merely an instance of the entity class from the
context, it's an anonymous type from a custom linq statement.
 
L

Lloyd Sheen

david said:
thanks, I appreciate it, but I do realize that if I don't have an
anonymous type I can cast the DataItem to the type it is.

The problem occurs when I *do* have an anonymous type: in this
particular case because I've set the Select property on the DataSource
but the same thing happens if I do something like add a join to the query.

In those cases, the above is just going to return null, since
e.Row.DataItem is not merely an instance of the entity class from the
context, it's an anonymous type from a custom linq statement.
What I do in that case is create a simple class that encapsulates the
members of anonymous type. Then rather than an anonymous type use that
class as the return type (list of).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top