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

5 posts, 0 answers
  1. Erik Lidman
    Erik Lidman avatar
    14 posts
    Member since:
    Feb 2008

    Posted 01 Sep 2010 Link to this post

    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
  2. Cori
    Cori avatar
    562 posts
    Member since:
    Jul 2010

    Posted 01 Sep 2010 Link to this post

    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.
  3. Erik Lidman
    Erik Lidman avatar
    14 posts
    Member since:
    Feb 2008

    Posted 01 Sep 2010 Link to this post

    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();
  4. Yana
    Admin
    Yana avatar
    4848 posts

    Posted 07 Sep 2010 Link to this post

    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
  5. Erik Lidman
    Erik Lidman avatar
    14 posts
    Member since:
    Feb 2008

    Posted 15 Sep 2010 Link to this post

    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

Back to Top