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

Parent data source

3 Answers 82 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Leszek
Top achievements
Rank 1
Leszek asked on 17 Mar 2015, 08:50 PM
Hi,

I'd like to access the parent data source from a column template. Something like this:

<telerik:GridTemplateColumn DataField="Number" HeaderText="Number">
    <ItemTemplate>
        <%# ((MyCollection)Parent.DataSource).MyProperty %>
    </ItemTemplate>
</telerik:GridTemplateColumn>

where MyCollection represents a class I use to populate RadGrid and MyProperty is a property of this class that I'd like to access.

Is it possible to accomplish that?

Thanks,
Leszek

3 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 20 Mar 2015, 04:31 PM
Hi Leszek,

Generally, the respective GridDataItem in this context is called Container:
<telerik:GridTemplateColumn DataField="Number" HeaderText="Number">
    <ItemTemplate>
        <%#  DataBinder.Eval(Container.DataItem, "Freight") %>
    </ItemTemplate>
</telerik:GridTemplateColumn>

In this scenario you can also use the ItemDataBound event handler to execute any custom logic.

Hope this helps.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Leszek
Top achievements
Rank 1
answered on 26 Mar 2015, 01:05 PM
Thanks Eyup for your response. Maybe I was not clear enough: what I need is access to the 'parent' (i.e., a collection used to populate the grid) rather than the item itself. Is the "Container" what I'm looking for? Following your example, would the following code work? :

<telerik:GridTemplateColumn DataField="Number" HeaderText="Number">
    <ItemTemplate>
        <%#  DataBinder.Eval(((MyCollection)Container), "MyProperty") %>
    </ItemTemplate>
</telerik:GridTemplateColumn>

The idea is to bind to a property of the collection itself rather than to its data item.

Thanks,
Leszek

0
Eyup
Telerik team
answered on 31 Mar 2015, 10:31 AM
Hello Leszek,

In this case you can try to use a programmatic approach:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        var dataItem = e.Item.DataItem;
        // execute custom logic to access the underlying data
    }
}

Once you have the value, you can set the Text of the cell or change the property of a template control using FindControl, e.g. Label.

That should do the trick.

Regards,
Eyup
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
Grid
Asked by
Leszek
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Leszek
Top achievements
Rank 1
Share this question
or