473,379 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

LinqDataSource and GridView.RowDataBound

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
Jul 26 '08 #1
3 6435
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

Jul 26 '08 #2
Md. Masudur Rahman wrote:
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.

Jul 26 '08 #3
david wrote:
Md. Masudur Rahman wrote:
>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.
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).
Jul 26 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: danc | last post by:
I am trying to modify the layout of a row based on the value in some data (I am creating a subtotal line). This works fine when the page is first loaded, but when it's reloaded, the wrong column is...
0
by: tfsmag | last post by:
I'm having a weird problem with the rowdatabound event for a gridview in my application... Here is the code. -------------------------------------- Protected Sub AnswersGrid_RowDataBound(ByVal...
1
by: K B | last post by:
Hi, I'm using the following to change a text label in a specific row/control. This works great until I click the Edit commandfield. then RowDataBound can't find the Label control because it is now...
5
by: sutphinwb | last post by:
Hi - This could be a simple question. When I relate two tables in a datasetet, how do I get that relation to show up in a GridView? The only way I've done it, is to create a separate table in the...
3
by: ryan.mclean | last post by:
Hello everyone, I'm in a bind, and I hope somebody can point me in the right direction. I have a gridview that is bound when it is not a postback. When the grid is bound, I remove the contents...
1
by: David W | last post by:
I have a pretty standard bound GridView control with TemplateFields. Now I need to be able to control the properties of some of the columns based on values in the recordset that I am NOT...
3
by: David C | last post by:
My brain is fried. How can I determine if the row is in edit mode when processing the RowDataBound event? Thanks. David
17
by: ata | last post by:
Hi folks, Consider a gridview that's being bound to a ObjectDataSource that uses an adapter to handle data access logic. 1. How am I supposed to have a RowIndex column *without* modifying the...
1
by: pvong | last post by:
Doing this in VB.Net I have Gridview where Column22 has a LinkButton (BlockLB) that has Visable=False. I have a RowDataBound event and it's doing some test. All I want to do is make the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.