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

Nested View Template in Grid Not Firing DetailTableDataBind

8 Answers 395 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brett
Top achievements
Rank 1
Brett asked on 25 Oct 2012, 04:59 AM
I have the following markup to create a grid with a nested view template and I cannot get the DetailTableDataBind event to fire.  Could someone please review my code and walk me through the steps needed to get the detail tables to bind programmatically?

Here is my .aspx markup and code behind:

 

<telerik:RadGrid ID="RadGrid" OnItemCreated="RadGrid_ItemCreated" OnItemCommand="RadGrid_ItemCommand" OnPreRender="RadGrid_PreRender" runat="server" AllowSorting="true" AllowPaging="false" AutoGenerateColumns="false" AllowFilteringByColumn="true" OnNeedDataSource="RadGrid_NeedDataSource" OnDetailTableDataBind="RadGrid_DetailTableDataBind">
        <MasterTableView AutoGenerateColumns="false" HierarchyLoadMode="ServerBind" AllowFilteringByColumn="true" ShowFooter="true" AllowPaging="false" AllowMultiColumnSorting="true" DataKeyNames="UnitId">
             
            <NestedViewSettings>
                <telerik:ParentTableRelation>
                    <telerik:GridRelationFields MasterKeyField="UnitId" DetailKeyField="UnitId" />
                </telerik:ParentTableRelation>
            </NestedViewSettings>
            <NestedViewTemplate>
                <asp:Panel runat="server" ID="InnerContainer" CssClass="viewWrap" Visible="false">
                    <telerik:RadTabStrip runat="server" ID="TabStip1" MultiPageID="Multipage1" SelectedIndex="0">
                        <Tabs>
                            <telerik:RadTab runat="server" Text="Orders" PageViewID="PageView1">
                            </telerik:RadTab>
 
                        </Tabs>
                    </telerik:RadTabStrip>
                    <telerik:RadMultiPage runat="server" ID="Multipage1" SelectedIndex="0" RenderSelectedPageOnly="false">
                        <telerik:RadPageView runat="server" ID="PageView1">
                            <telerik:RadGrid runat="server" ID="Orders" ShowFooter="true"
                                AllowSorting="true" EnableLinqExpressions="false" OnDetailTableDataBind="RadGrid_DetailTableDataBind">
                                <MasterTableView HierarchyLoadMode="ServerOnDemand" ShowHeader="true" DataKeyNames="UnitId" AutoGenerateColumns="false" AllowFilteringByColumn="true" ShowFooter="true" AllowPaging="false" AllowMultiColumnSorting="true">
                                    <Columns>
                                        <telerik:GridBoundColumn SortExpression="UnitId" HeaderText="UnitId" HeaderButtonType="TextButton"
                                            DataField="UnitId" UniqueName="UnitId">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn SortExpression="Date" HeaderText="Date" HeaderButtonType="TextButton"
                                            DataField="Date" UniqueName="Date">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn SortExpression="BillNumber" HeaderText="Bill Number" HeaderButtonType="TextButton"
                                            DataField="BillNumber" UniqueName="BillNumber">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn SortExpression="Status" HeaderText="Status" HeaderButtonType="TextButton"
                                            DataField="Status" UniqueName="Status">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn SortExpression="Origin" HeaderText="Origin" HeaderButtonType="TextButton"
                                            DataField="Origin" UniqueName="Origin">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn SortExpression="Destination" HeaderText="Destination" HeaderButtonType="TextButton"
                                            DataField="Destination" UniqueName="OrDestinationigin">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridNumericColumn NumericType="Currency" Aggregate="Sum" FooterAggregateFormatString="{0:C}" DataField="XCharges" HeaderText="Acc. Charges" SortExpression="XCharges" UniqueName="XCharges" DataType="System.Decimal"  />
                                        <telerik:GridNumericColumn NumericType="Currency" Aggregate="Sum" FooterAggregateFormatString="{0:C}" DataField="TotalCharge" HeaderText="Total Charges" SortExpression="TotalCharge" UniqueName="TotalCharge" DataType="System.Decimal"  />
                                    </Columns>
                                </MasterTableView>
                            </telerik:RadGrid>
                             
                        </telerik:RadPageView>
                    </telerik:RadMultiPage>
                </asp:Panel>
            </NestedViewTemplate>
            <Columns>
                <telerik:GridBoundColumn HeaderStyle-Width="10" DataField="Division" HeaderText="Division" SortExpression="Division" UniqueName="Division" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" />
                <telerik:GridBoundColumn DataField="Subdivision" HeaderText="Subdivision" SortExpression="Subdivision" UniqueName="Subdivision" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" />
                <telerik:GridBoundColumn DataField="UnitId" HeaderText="Unit" SortExpression="UnitId" UniqueName="UnitId" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Sum" FooterAggregateFormatString="{0:C}" DataField="GrossRevenue" HeaderText="Revenue" SortExpression="GrossRevenue" UniqueName="GrossRevenue" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Sum" FooterAggregateFormatString="{0:C}" DataField="TotalPay" HeaderText="Driver Pay" SortExpression="TotalPay" UniqueName="TotalPay" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Sum" FooterAggregateFormatString="{0:C}" DataField="TotalFuel" HeaderText="Fuel Cost" SortExpression="TotalFuel" UniqueName="TotalFuel" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Sum" FooterAggregateFormatString="{0:C}" DataField="TotalMaintenance" HeaderText="Maint. Cost" SortExpression="TotalMaintenance" UniqueName="TotalMaintenance" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Sum" FooterAggregateFormatString="{0:C}" DataField="Contribution" HeaderText="Contribution" SortExpression="Contribution" UniqueName="Contribution" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Avg" FooterAggregateFormatString="{0:C}" DataField="TotalCPM" HeaderText="Total CPM" SortExpression="TotalCPM" UniqueName="TotalCPM" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Avg" FooterAggregateFormatString="{0:C}" DataField="PayCPM" HeaderText="Pay CPM" SortExpression="PayCPM" UniqueName="PayCPM" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Avg" FooterAggregateFormatString="{0:C}" DataField="FuelCPM" HeaderText="Fuel CPM" SortExpression="FuelCPM" UniqueName="FuelCPM" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Avg" FooterAggregateFormatString="{0:C}" DataField="MaintenanceCPM" HeaderText="Maint. CPM" SortExpression="MaintenanceCPM" UniqueName="MaintenanceCPM" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Number" DataFormatString="{0:N2}" Aggregate="Sum" FooterAggregateFormatString="{0:N2}" DataField="TotalMiles" HeaderText="Total Miles" SortExpression="TotalMiles" UniqueName="TotalMiles" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Number" DataFormatString="{0:N2}" Aggregate="Avg" FooterAggregateFormatString="{0:N2}" DataField="MPG" HeaderText="MPG" SortExpression="MPG" UniqueName="MPG" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
public partial class UtilizationReport : System.Web.UI.Page
    {
        private BTReports.Repository.UnitUtilizationRepository rep = new Repository.UnitUtilizationRepository();
 
        protected void Page_Load(object sender, EventArgs e)
        {          
            
        }
        protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (Cache["UtilzationData"] == null)
            {
                if (StartDatePicker.SelectedDate.HasValue && EndDatePicker.SelectedDate.HasValue)
                {
                    RadGrid.DataSource = rep.GetUnitUtilization(StartDatePicker.SelectedDate.Value, EndDatePicker.SelectedDate.Value);
                }
            }
            else
            {
                RadGrid.DataSource = Cache["UtilzationData"];
            }
        }
 
 
        protected void GenerateButton_Click(object sender, EventArgs e)
        {
            Cache.Remove("UtilzationData");
            Cache["UtilzationData"] = rep.GetUnitUtilization(StartDatePicker.SelectedDate.Value, EndDatePicker.SelectedDate.Value);
             
            RadGrid.DataSource = rep.GetUnitUtilization(StartDatePicker.SelectedDate.Value, EndDatePicker.SelectedDate.Value);
            RadGrid.DataBind();
             
        }
 
        protected void RadGrid_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
        {
            GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
            switch (e.DetailTableView.Name)
            {
                case "Orders":
                    {
                        DataTable table = new DataTable();
                        table.Columns.Add("UnitId", typeof(string));
                        table.Columns.Add("Date", typeof(DateTime));
                        table.Columns.Add("BillNumber", typeof(string));
                        table.Columns.Add("Status", typeof(string));
                        table.Columns.Add("Origin", typeof(string));
                        table.Columns.Add("Destination", typeof(string));
                        table.Columns.Add("XCharge", typeof(decimal));
                        table.Columns.Add("TotalCharges", typeof(decimal));
 
                        string UnitId = dataItem.GetDataKeyValue("UnitId").ToString();
                        IQueryable<UnitUtilization> data = (IQueryable<UnitUtilization>)Cache["UtilzationData"];
                        var results = from i in data
                                      where i.UnitId == UnitId
                                      select i.Orders;
 
                        foreach (var order in results)
                        {
                            foreach (Order o in order)
                            {
                                table.Rows.Add(o.UnitId, o.Date, o.BillNumber, o.Status, o.Origin, o.Destination, o.XCharges, o.TotalCharges);
                            }
                        }
                        e.DetailTableView.DataSource = table;
                        break;
                    }
 
            }
        }
 
        protected void RadGrid_PreRender(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //RadGrid.MasterTableView.Items[0].Expanded = true;
                //RadGrid.MasterTableView.Items[0].ChildItem.FindControl("InnerContainer").Visible = true;
            }
        }
 
        protected void RadGrid_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.ExpandCollapseCommandName && e.Item is GridDataItem)
            {
                ((GridDataItem)e.Item).ChildItem.FindControl("InnerContainer").Visible =
                    !e.Item.Expanded;
            }
        }
 
        protected void RadGrid_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridNestedViewItem)
            {
                e.Item.FindControl("InnerContainer").Visible = ((GridNestedViewItem)e.Item).ParentItem.Expanded;
                 
            }
        }
 
        protected void ExportButton_Click(object sender, System.EventArgs e)
        {
            RadGrid.MasterTableView.ExportToExcel();
        }
 
        protected void ChartButton_Click(object sender, EventArgs e)
        {
            foreach (GridDataItem item in RadGrid.MasterTableView.Items)
            {
                string UnitId = item["UnitId"].ToString();
            }
        }
    }

8 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Oct 2012, 08:29 AM
Hi,

DetailTableDataBind event is fired when binding each detail table view in hierarchy. You can bind the inner grid(nested) using its NeedDataSource event. Also check the following demo.
Grid / NestedViewTemplate Relations

Thanks,
Shinu.
0
Brett
Top achievements
Rank 1
answered on 25 Oct 2012, 02:23 PM
Thank you for your reply.  How would I access the GetDataKeyValue, inside of the OnNeededDataSource event, to be able to bind the grid using the correct key for the selected grid line?

Also, the NeedDataSource event is not firing on the grid in the view template.

Thanks for all your help!
0
Shelly
Top achievements
Rank 1
answered on 25 Oct 2012, 05:07 PM
I hope you get another response to this -- you asked exactly the questions I need to know the answer for too.  The demo
Grid / NestedViewTemplate Relations helps a little bit, but I would really like to see an example with a RadTabStrip inside the nested view, with a RadGrid in the pageview for that.  

Thanks,
Shelly
0
Shinu
Top achievements
Rank 2
answered on 26 Oct 2012, 04:37 AM
Hi Brett,

NeedDataSource event is firing at my end. Here is the sample code to access the datakeyvalue.
C#:
protected void RadGrid2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
  RadGrid innergrid=(RadGrid)sender;
  GridNestedViewItem nesteditem = (GridNestedViewItem)innergrid.NamingContainer;
 string dataKeyValue = Convert.ToString(((GridDataItem)(nesteditem.ParentItem)).GetDataKeyValue("ID"));
}

Thanks,
Shinu.
0
Brett
Top achievements
Rank 1
answered on 26 Oct 2012, 05:30 PM
Shinu,
    Thank you for your reponse.  I was able to get the NeedDataSource event to fire, but the dataKeyValue field is an empty string.  The dataKeyValue of the expanded row isn't being set ( please see attached image).  Could you please let me know what I need to do to get the selected dataKeyValue to populate correctly?

Thanks again for all your help!
Brett

Here is all the code currently used:

ASPX:
<telerik:RadGrid ID="RadGrid" OnItemCreated="RadGrid_ItemCreated" OnItemCommand="RadGrid_ItemCommand" OnPreRender="RadGrid_PreRender" runat="server" AllowSorting="true" AllowPaging="false" AutoGenerateColumns="false" AllowFilteringByColumn="true" OnNeedDataSource="RadGrid_NeedDataSource">
  
        <MasterTableView AutoGenerateColumns="false" HierarchyLoadMode="ServerBind" AllowFilteringByColumn="true" ShowFooter="true" AllowPaging="false" AllowMultiColumnSorting="true" DataKeyNames="UnitId">
  
              
            <NestedViewSettings>
  
                <telerik:ParentTableRelation>
  
                    <telerik:GridRelationFields MasterKeyField="UnitId" DetailKeyField="UnitId" />
  
                </telerik:ParentTableRelation>
  
            </NestedViewSettings>
  
            <NestedViewTemplate>
  
                <asp:Panel runat="server" ID="InnerContainer" CssClass="viewWrap" Visible="false">
  
                    <telerik:RadTabStrip runat="server" ID="TabStip1" MultiPageID="Multipage1" SelectedIndex="0">
  
                        <Tabs>
  
                            <telerik:RadTab runat="server" Text="Orders" PageViewID="PageView1">
  
                            </telerik:RadTab>
  
  
  
                        </Tabs>
  
                    </telerik:RadTabStrip>
  
                    <telerik:RadMultiPage runat="server" ID="Multipage1" SelectedIndex="0" RenderSelectedPageOnly="false">
  
                        <telerik:RadPageView runat="server" ID="PageView1">
  
                            <telerik:RadGrid runat="server" ID="RadGrid2" ShowFooter="true"
  
                                AllowSorting="true" EnableLinqExpressions="false" OnNeedDataSource="RadGrid2_NeedDataSource">
  
                                <MasterTableView HierarchyLoadMode="ServerBind" ShowHeader="true" DataKeyNames="UnitId" AutoGenerateColumns="false" AllowFilteringByColumn="true" ShowFooter="true" AllowPaging="false" AllowMultiColumnSorting="true">
  
                                    <Columns>
  
                                        <telerik:GridBoundColumn SortExpression="UnitId" HeaderText="UnitId" HeaderButtonType="TextButton"
  
                                            DataField="UnitId" UniqueName="UnitId">
  
                                        </telerik:GridBoundColumn>
  
                                        <telerik:GridBoundColumn SortExpression="Date" HeaderText="Date" HeaderButtonType="TextButton"
  
                                            DataField="Date" UniqueName="Date">
  
                                        </telerik:GridBoundColumn>
  
                                        <telerik:GridBoundColumn SortExpression="BillNumber" HeaderText="Bill Number" HeaderButtonType="TextButton"
  
                                            DataField="BillNumber" UniqueName="BillNumber">
  
                                        </telerik:GridBoundColumn>
  
                                        <telerik:GridBoundColumn SortExpression="Status" HeaderText="Status" HeaderButtonType="TextButton"
  
                                            DataField="Status" UniqueName="Status">
  
                                        </telerik:GridBoundColumn>
  
                                        <telerik:GridBoundColumn SortExpression="Origin" HeaderText="Origin" HeaderButtonType="TextButton"
  
                                            DataField="Origin" UniqueName="Origin">
  
                                        </telerik:GridBoundColumn>
  
                                        <telerik:GridBoundColumn SortExpression="Destination" HeaderText="Destination" HeaderButtonType="TextButton"
  
                                            DataField="Destination" UniqueName="OrDestinationigin">
  
                                        </telerik:GridBoundColumn>
  
                                        <telerik:GridNumericColumn NumericType="Currency" Aggregate="Sum" FooterAggregateFormatString="{0:C}" DataField="XCharges" HeaderText="Acc. Charges" SortExpression="XCharges" UniqueName="XCharges" DataType="System.Decimal"  />
  
                                        <telerik:GridNumericColumn NumericType="Currency" Aggregate="Sum" FooterAggregateFormatString="{0:C}" DataField="TotalCharge" HeaderText="Total Charges" SortExpression="TotalCharge" UniqueName="TotalCharge" DataType="System.Decimal"  />
  
                                    </Columns>
  
                                </MasterTableView>
  
                            </telerik:RadGrid>
  
                              
  
                        </telerik:RadPageView>
  
                    </telerik:RadMultiPage>
  
                </asp:Panel>
  
            </NestedViewTemplate>
            <Columns>
                <telerik:GridBoundColumn HeaderStyle-Width="10" DataField="Division" HeaderText="Division" SortExpression="Division" UniqueName="Division" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" />
                <telerik:GridBoundColumn DataField="Subdivision" HeaderText="Subdivision" SortExpression="Subdivision" UniqueName="Subdivision" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" />
                <telerik:GridBoundColumn DataField="UnitId" HeaderText="Unit" SortExpression="UnitId" UniqueName="UnitId" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Sum" FooterAggregateFormatString="{0:C}" DataField="GrossRevenue" HeaderText="Revenue" SortExpression="GrossRevenue" UniqueName="GrossRevenue" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Sum" FooterAggregateFormatString="{0:C}" DataField="TotalPay" HeaderText="Driver Pay" SortExpression="TotalPay" UniqueName="TotalPay" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Sum" FooterAggregateFormatString="{0:C}" DataField="TotalFuel" HeaderText="Fuel Cost" SortExpression="TotalFuel" UniqueName="TotalFuel" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Sum" FooterAggregateFormatString="{0:C}" DataField="TotalMaintenance" HeaderText="Maint. Cost" SortExpression="TotalMaintenance" UniqueName="TotalMaintenance" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Sum" FooterAggregateFormatString="{0:C}" DataField="Contribution" HeaderText="Contribution" SortExpression="Contribution" UniqueName="Contribution" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Avg" FooterAggregateFormatString="{0:C}" DataField="TotalCPM" HeaderText="Total CPM" SortExpression="TotalCPM" UniqueName="TotalCPM" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Avg" FooterAggregateFormatString="{0:C}" DataField="PayCPM" HeaderText="Pay CPM" SortExpression="PayCPM" UniqueName="PayCPM" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Avg" FooterAggregateFormatString="{0:C}" DataField="FuelCPM" HeaderText="Fuel CPM" SortExpression="FuelCPM" UniqueName="FuelCPM" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Currency" Aggregate="Avg" FooterAggregateFormatString="{0:C}" DataField="MaintenanceCPM" HeaderText="Maint. CPM" SortExpression="MaintenanceCPM" UniqueName="MaintenanceCPM" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Number" DataFormatString="{0:N2}" Aggregate="Sum" FooterAggregateFormatString="{0:N2}" DataField="TotalMiles" HeaderText="Total Miles" SortExpression="TotalMiles" UniqueName="TotalMiles" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
                <telerik:GridNumericColumn NumericType="Number" DataFormatString="{0:N2}" Aggregate="Avg" FooterAggregateFormatString="{0:N2}" DataField="MPG" HeaderText="MPG" SortExpression="MPG" UniqueName="MPG" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="true" DataType="System.Decimal"  />
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>



Code Behind:
public partial class UtilizationReport : System.Web.UI.Page
    {
        private BTReports.Repository.UnitUtilizationRepository rep = new Repository.UnitUtilizationRepository();
 
        protected void Page_Load(object sender, EventArgs e)
        {          
            
        }
 
        protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (Cache["UtilzationData"] == null)
            {
                if (StartDatePicker.SelectedDate.HasValue && EndDatePicker.SelectedDate.HasValue)
                {
                    RadGrid.DataSource = rep.GetUnitUtilization(StartDatePicker.SelectedDate.Value, EndDatePicker.SelectedDate.Value);
                }
            }
            else
            {
                RadGrid.DataSource = Cache["UtilzationData"];
            }
        }
       
        protected void GenerateButton_Click(object sender, EventArgs e)
        {
            Cache.Remove("UtilzationData");
            Cache["UtilzationData"] = rep.GetUnitUtilization(StartDatePicker.SelectedDate.Value, EndDatePicker.SelectedDate.Value);
             
            RadGrid.DataSource = rep.GetUnitUtilization(StartDatePicker.SelectedDate.Value, EndDatePicker.SelectedDate.Value);
            RadGrid.DataBind();
             
        }
 
        protected void RadGrid2_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            RadGrid innergrid = (RadGrid)source;
 
            GridNestedViewItem nesteditem = (GridNestedViewItem)innergrid.NamingContainer;
 
            string dataKeyValue = Convert.ToString(((GridDataItem)(nesteditem.ParentItem)).GetDataKeyValue("ID"));
 
            DataTable table = new DataTable();
 
            table.Columns.Add("UnitId", typeof(string));
 
            table.Columns.Add("Date", typeof(DateTime));
 
            table.Columns.Add("BillNumber", typeof(string));
 
            table.Columns.Add("Status", typeof(string));
 
            table.Columns.Add("Origin", typeof(string));
 
            table.Columns.Add("Destination", typeof(string));
 
            table.Columns.Add("XCharge", typeof(decimal));
 
            table.Columns.Add("TotalCharges", typeof(decimal));
 
 
 
            string UnitId = dataKeyValue;
 
            IQueryable<UnitUtilization> data = (IQueryable<UnitUtilization>)Cache["UtilzationData"];
 
            var results = from i in data
 
                          where i.UnitId == UnitId
 
                          select i.Orders;
 
 
 
            foreach (var order in results)
            {
 
                foreach (Order o in order)
                {
 
                    table.Rows.Add(o.UnitId, o.Date, o.BillNumber, o.Status, o.Origin, o.Destination, o.XCharges, o.TotalCharges);
 
                }
 
            }
 
            innergrid.DataSource = table;
 
        }
 
        protected void RadGrid_PreRender(object sender, EventArgs e)
        {
 
            if (!Page.IsPostBack)
            {
 
                //RadGrid.MasterTableView.Items[0].Expanded = true;
 
                //RadGrid.MasterTableView.Items[0].ChildItem.FindControl("InnerContainer").Visible = true;
 
            }
 
        }
         
        protected void RadGrid_ItemCommand(object source, GridCommandEventArgs e)
        {
 
            if (e.CommandName == RadGrid.ExpandCollapseCommandName && e.Item is GridDataItem)
            {
 
                ((GridDataItem)e.Item).ChildItem.FindControl("InnerContainer").Visible =
 
                    !e.Item.Expanded;
 
            }
 
        }
         
        protected void RadGrid_ItemCreated(object sender, GridItemEventArgs e)
        {
 
            if (e.Item is GridNestedViewItem)
            {
 
                e.Item.FindControl("InnerContainer").Visible = ((GridNestedViewItem)e.Item).ParentItem.Expanded;
 
 
 
            }
 
        }
 
        protected void ExportButton_Click(object sender, System.EventArgs e)
        {
            RadGrid.MasterTableView.ExportToExcel();
        }
 
        protected void ChartButton_Click(object sender, EventArgs e)
        {
            foreach (GridDataItem item in RadGrid.MasterTableView.Items)
            {
                string UnitId = item["UnitId"].ToString();
            }
        }
    }
0
Brett
Top achievements
Rank 1
answered on 26 Oct 2012, 08:14 PM
I was able to fix my previous issue the datakey.  Now I'm having an issue with the timinig/firing of the NeedDataSource event.  The only time the event is firing is when I collapse a row.  When I click on the arrow to expand a row, the event doesn't fire.  If I collpase an expanded row the event fires.  What can I do differently to cause the event to fire when the row is expanded?
Thanks
Brett
0
Shelly
Top achievements
Rank 1
answered on 29 Oct 2012, 01:58 PM
Brett,

I am still working through the same problems. I did manage to make the InnerGrid NeedDataSource event fire on expand a row in the mainGrid.  Here's what I ended up with.  I'm not sure how correct it is, but it does work:

protected void MainRadGrid_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.ExpandCollapseCommandName && e.Item is GridDataItem)
            {
                ((GridDataItem)e.Item).ChildItem.FindControl("InnerContainer").Visible = true;
                ((e.Item as GridDataItem).ChildItem.FindControl("InnerGrid") as RadGrid).Rebind();
            }
        }

My new problem is that I'd like the first row of the main grid to be expanded when the page loads, but I can't get NeedDataSource for the InnerGrid to fire in that case.  I think it's a timing issue as well, I can't get the InnerGrid initialized on the page in that scenario, which then prevents NeedDataSource for it from firing.

Shelly
0
Angel Petrov
Telerik team
answered on 31 Oct 2012, 12:44 PM
Hi Shelly,

You are on the right path to resolve this case. In order to call the inner grid NeedDataSource you have to expand the first row in the MasterTableView in an early event such as ItemCreated. I suggest that you take a look at the following demo which implements your scenario. As you can see in the demo the RadGrid's first row is being expanded in the ItemCreated event which comes first. Then the inner grid NeedDataSource event is being fired to populate the RadGrid in the NestedViewTemplate. In order to call the inner grid NeedDataSource event later on you need to intercept the expand or collapse event and call the Rebind() as you have previously suggested. Hope this information helps.

All the best,
Angel Petrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Brett
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Brett
Top achievements
Rank 1
Shelly
Top achievements
Rank 1
Angel Petrov
Telerik team
Share this question
or