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

NestedViewTemplate HELP Please

4 Answers 340 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 12 Jun 2008, 04:20 PM
OK, I know that I can use the NestedViewTemplate and from the code in that template reference values of the MainView data item.  I have the example of using a NestedViewTemplate which contains another Grid and the gird in the template is bound to a DataSource server control.

I do not want to use SqlDataSource or any other data source control as my implementation is not utilizing these controls. I need to bind my grid in the NextedViewTemplate via code behind.

Problem, for testing I have an ASP:Label control that I am setting the Text property to a value from the Master Grid which works fine but, in the code behind event of RADGRID_ItemCreated when the NestedViewItem is the item being created the label control has not had it's Text property assigned yet.

Is there a more appropriate event I can use to bind my grid in the NestedViewTemplate to a value based on the corresponding row in the Master Table.

Here is a snippet of my current ASPX code:
<NestedViewTemplate> 
                    SessionID: <asp:Label ID="lblSessionID" runat="server" Text='<%# Eval("SessionID") %>' /><br /> 
                    Race Day ID:<asp:Label ID="lblRaceDayID" runat="server" Text='<%# Eval("RaceDayID") %>' /><br />                     
                                             
                            <telerik:RadGrid  
                                ID="rgLaps"  
                                runat="server"  
                                Skin="Default"
                                <MasterTableView  
                                    DataKeyNames="SessionLapID" 
                                    CommandItemDisplay="Top" 
                                    EditMode="InPlace"
                                    <CommandItemSettings AddNewRecordText="Add New Lap" RefreshText="Refresh" /> 
                                    <Columns> 
                                        <telerik:GridButtonColumn  
                                            UniqueName="LapsDeleteColumn"  
                                            Text="Delete"  
                                            CommandName="Delete" /> 
                                        <telerik:GridBoundColumn 
                                            UniqueName="LapNumber" 
                                            DataField="Number" 
                                            HeaderText="Lap Number" /> 
                                        <telerik:GridBoundColumn 
                                            UniqueName="LapTime" 
                                            DataField="Time" 
                                            HeaderText="Lap Time" /> 
                                        <telerik:GridBoundColumn 
                                            UniqueName="LapIgnore" 
                                            DataField="Ignore" 
                                            HeaderText="Ignore Lap" />                                                                                                                                     
                                        <telerik:GridEditCommandColumn  
                                            UniqueName="LapsEditCommandColumn"  
                                            CancelText="Cancel"  
                                            EditText="Edit"  
                                            InsertText="Insert"  
                                            UpdateText="Update" />                                         
                                    </Columns> 
                                    <EditFormSettings  
                                        CaptionFormatString="Edit details for lap with ID {0}"  
                                        CaptionDataField="SessionLapID"
                                        <FormTableItemStyle Width="100%" Height="29px" /> 
                                        <FormTableStyle GridLines="None" CellSpacing="0" CellPadding="2" /> 
                                        <FormStyle Width="100%" BackColor="#eef2ea" /> 
                                        <EditColumn ButtonType="ImageButton" /> 
                                    </EditFormSettings> 
                                </MasterTableView> 
                            </telerik:RadGrid> 
 
</NestedViewTemplate> 

Here is a snippet of the code from my code behind:
protected void rgRaceDaySessions_ItemCreated(object sender, GridItemEventArgs e) { 
      if (e.Item.ItemType == GridItemType.NestedView) { 
        // Bind grid that shows Laps. 
        RadGrid gridLaps = (RadGrid)e.Item.FindControl("rgLaps"); 
        Label lbl = (Label)e.Item.FindControl("lblSessionID"); 
        Label lblRace = (Label)e.Item.FindControl("lblRaceDayID"); 
 
        int sessionID = Utils.GetInteger(lbl.Text); 
        int raceID = Utils.GetInteger(lblRace.Text); 
 
        if (sessionID > 0) { 
            // Bind grid in nested view to data for SessionID being bound in Main View. 
        } 
      } 
    } 


Again, I do not want to use a DataSource server control in the ASPX page to do this binding.

4 Answers, 1 is accepted

Sort by
0
Eric
Top achievements
Rank 1
answered on 13 Jun 2008, 06:23 PM
Kinda surprised that I am not getting any reply here.

All I need to know now is how to determine which Master Table row is being bound when the NestedViewTemplate is being created.

The e.Item.OwnerTableView.DataKeyValues array does have an array item for each id that I need.  The problem is knowing the correct index to use into the array during the RADGRID_ItemCreated event when the type of the item being created is GridItemType.NestedView. 

I have looked at all of the properties in e.Item and e.Item.OwnerTableView to try and figure out which row index in the master table the NestedView being created belongs to and have not had any luck.

Please Help....

Thank you
0
Vlad
Telerik team
answered on 16 Jun 2008, 06:13 AM
Hello Eric,

Generally you can use ItemDataBound to achieve your goal (e.Item.DataItem is available). DataItem for the NestedTableView is the DataItem for parent GridDataItem and you can access all properties using DataBinding.Eval() or if you cast e.Item.DataItem to your type.

Kind regards,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Shinu
Top achievements
Rank 2
answered on 16 Jun 2008, 07:11 AM
Hi Eric,

You can also have a look at the following help article.
Distinguish grid rows in hierarchy on ItemCreated/ItemDataBound

Shinu.
0
Karl
Top achievements
Rank 1
answered on 24 Jan 2011, 11:42 AM
I'm not sure if you ever got this to work or not, but thought I'd post my solution to this just incase. If you got it working by another means, or if you see an issue with my code, please let me know...

I have a RadGrid (OuterGrid) which has summary information about my item, and each row has a NestedViewTemplate which haas another RadGrid (InnerGrid) which has more details information over multiple lines. My datasource for everything is a list of objects, and the InnerGrid'd data comes for a list property ineach object. The data is first loaded and saved into the viewstate when the page first loads, and picked up from viewstate everytime the page postsback.

Now. My solution...

protected void GetOuterDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    OuterGrid.DataSource = logSessions.Values;
}
  
protected void OuterGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.ItemType == GridItemType.NestedView)
    {
        GridNestedViewItem NestedView = (GridNestedViewItem)e.Item;
        if (NestedView != null)
        {
            RadGrid InnerGrid = (RadGrid)NestedView.FindControl("InnerGrid");
            if (InnerGrid != null)
            {
                if (e.Item.DataItem != null)
                {
                    InnerGrid.DataSource = ((LogSession)e.Item.DataItem).Events;
                    InnerGrid.DataBind();
                }
            }
        }
    }
}
Tags
Grid
Asked by
Eric
Top achievements
Rank 1
Answers by
Eric
Top achievements
Rank 1
Vlad
Telerik team
Shinu
Top achievements
Rank 2
Karl
Top achievements
Rank 1
Share this question
or