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

How to extract DataItem column in NodeDataBound when based on EF object?

4 Answers 81 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Erik Lidman
Top achievements
Rank 1
Erik Lidman asked on 01 Sep 2010, 06:53 PM
Hi,

I have a RadTreeView that is populated with data using linq;
---
var sobjects = from so in context.Source
                           select new { so.ID, so.Name, so.Parent_ID, StatusID=so.Status_1.ID };

            SourceRadTreeView.DataSource = sobjects;
---
I then want to extract the StatusID value in the NodeDataBound event for each node.
e.Node.ToolTip = (e.Node.DataItem as DataRow)["StatusID"].ToString();
(as according to documentation)
But then I get;
Object reference not set to an instance of an object.

Solution?
/Erik Lidman

4 Answers, 1 is accepted

Sort by
0
Cori
Top achievements
Rank 2
answered on 01 Sep 2010, 07:47 PM
Hello Erik,

When it comes to binding using LINQ or EF, the DataItem is of the type you bound the data to. I notice you are creating an annoymous class, since your selecting the fields you want to display, but if you can select all the fields, returning a list of Source elements then you can implement it the following way, otherwise you can create a class that has those fields, so you can cast the DataItem object correctly. So if you use a list of Source objects you would cast the DataItem to Source, instead of a DataRow because DataRow is when your binding using SqlDataSource or a table object.

So your code would look like this instead:

e.Node.ToolTip = (e.Node.DataItem as Source).Status_1.ID.ToString();

I hope that helps.
0
Erik Lidman
Top achievements
Rank 1
answered on 01 Sep 2010, 10:22 PM
Hi,
Thanx for your reply.
I still have problems. After two hours of trying out a lot of different linq statements I still cant get it to work.

I put in the object Source (which is related to the entity Status).
In the NodeDataBound comes the object Source fine, but without the related information about Status.
/Erik

var sobjects =  from so in context.Source
select so;
 
SourceRadTreeView.DataSource = sobjects;
--------------------------------------------------------------------------------- 
protected void SourceRadTreeView_NodeDataBound(object sender, RadTreeNodeEventArgs e)
        {
            
            e.Node.ToolTip = (e.Node.DataItem as Source).Status.ID.ToString();
0
Yana
Telerik team
answered on 07 Sep 2010, 09:04 AM
Hi Erik,

We cannot reproduce the issue with the provided code. Can you please open a support ticket and send us simple runnable project (we don't need the whole database) demonstrating the problem? Thanks in advance

Kind regards,
Yana
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
Erik Lidman
Top achievements
Rank 1
answered on 15 Sep 2010, 09:03 PM
Solved it...
The linq query must return a shaped result, like this.

var sobjects =  from so in context.Source.
                               Include("Status").
                               Include("SourceType")
                            select so;

Case closed,
/Erik

Tags
TreeView
Asked by
Erik Lidman
Top achievements
Rank 1
Answers by
Cori
Top achievements
Rank 2
Erik Lidman
Top achievements
Rank 1
Yana
Telerik team
Share this question
or