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

Hierarchical RadGrid Calling Data Source Multiple Times

9 Answers 281 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jessica
Top achievements
Rank 1
Jessica asked on 04 Jun 2010, 06:42 PM
I have a RadGrid that is contained within a RadTabStrip/RadMultiPage/RadPageView.  This grid consists of a MasterTableView and two detail tables.  The data source for the grid is an ObjectDataSource and is being set in the OnNeedDataSource method.  The user enters a date range and clicks a button to populate the grid.  The button click event fires the grid.Rebind method.  The Rebind fires, calling the OnNeedDataSourceMethod.  After this method fires the select method of the ObjectDataSource gets called three times.

Is there a reason it's hitting the database three times?  Is there a way to get around this?  The dataset returns fairly quickly, but pulling it back three times is slowing down  the load of the grid.


9 Answers, 1 is accepted

Sort by
0
Francisco Gonzalez
Top achievements
Rank 1
answered on 04 Jun 2010, 10:54 PM
Hi,
How fill the detail tables?... with the same DataSource?

Tree times... One for the master table... and 2,3 for the detail tables.

Show me one pice of code... for help.
0
Jessica
Top achievements
Rank 1
answered on 07 Jun 2010, 10:01 PM
Each detail table has its own data source.  I now have the detail tables in a NestedViewTemplate and it looks like these are also being called multiple times.  A side note on this, the detail tables are being loaded by the ItemCommand for ExpandCollapse, but the data sources are called when the grid is initially populated, in addition to when the row is actually expanded.

I'm not sure what code exactly would be helpful.  Here are a few snippets:



        protected void GridWorkOrderEquipment_OnNeedDataSource(object source, GridNeedDataSourceEventArgs e) 
        { 
            string reason = e.RebindReason.ToString(); 
            if (e.RebindReason != GridRebindReason.InitialLoad) 
            { 
                selectingSource = "1"
                GridWorkOrderEquipment.DataSource = DataSourceCompletedWorkOrders; 
                                 
                TabStrip1.Visible = true
            }    
        } 
 
        protected void DataSourceCompletedWorkOrders_Selecting(object sender, ObjectDataSourceSelectingEventArgs e) 
        { 
            e.InputParameters["categoryId"] = selectingSource; 
            selectingCount++; 
        } 
                                    <telerik:RadPageView runat="server" ID="Equipment"
                                        <telerik:RadGrid ID="GridWorkOrderEquipment" runat="server" EnableViewState="true" AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True" OnNeedDataSource="GridWorkOrderEquipment_OnNeedDataSource" 
                                            GridLines="None" ShowGroupPanel="True" OnPageSizeChanged="GridWorkOrderEquipment_OnPageSizeChanged" Skin="WebBlue"
                                            <MasterTableView datakeynames="WorkOrderId" AllowSorting="true" OverrideDataSourceControlSorting="true" AutoGenerateColumns="false" PagerStyle-AlwaysVisible="true" ItemStyle-HorizontalAlign="Right" AlternatingItemStyle-HorizontalAlign="Right"
                                                <Columns> 
                                                    <%--<telerik:GridBoundColumn DataField = "WorkOrderId" HeaderText="WorkOrderId"></telerik:GridBoundColumn>--%> 
                                                    <telerik:GridBoundColumn DataField = "JobNumber" HeaderText="JobNumber"></telerik:GridBoundColumn> 
                                                    <telerik:GridBoundColumn DataField = "InstallDate" HeaderText="Install Date" DataFormatString="{0:d}"></telerik:GridBoundColumn> 
                                                    <telerik:GridBoundColumn DataField = "ExpectedAmount" HeaderText="ExpectedAmount" SortExpression="ExpectedAmount" DataFormatString="{0:C}"></telerik:GridBoundColumn> 
                                                    <telerik:GridBoundColumn DataField = "ActualAmount" HeaderText="ActualAmount" DataFormatString="{0:C}"></telerik:GridBoundColumn> 
                                                    <telerik:GridBoundColumn DataField = "PaymentAmount" HeaderText="PaidAmount" DataFormatString="{0:C}"></telerik:GridBoundColumn> 
                                                    <telerik:GridBoundColumn DataField="ExpectedActualDiff" HeaderText="Expected - Actual" DataFormatString="{0:C}"></telerik:GridBoundColumn> 
                                                    <telerik:GridBoundColumn DataField="ActualPaymentDiff" HeaderText="Actual - Payment" DataFormatString="{0:C}"></telerik:GridBoundColumn> 
                                                    <telerik:GridBoundColumn DataField="ExpectedPaymentDiff" HeaderText="Expected - Payment" DataFormatString="{0:C}"></telerik:GridBoundColumn> 
                                                </Columns> 
                                                <RowIndicatorColumn><HeaderStyle Width="20px"></HeaderStyle></RowIndicatorColumn
                                                <NestedViewTemplate> 
                                                    <asp:Panel runat="server"
                                                        <div> 
                                                            <table> 
                                                                <tr> 
                                                                    <td valign="top"
                                                                        <fieldset> 
                                                                            <legend>Expected</legend> 
                                                                            <telerik:RadGrid runat="server" ID="GridExpected" OnNeedDataSource="DetailsGrid_OnNeedDataSource" AllowSorting="true"
                                                                                <MasterTableView AutoGenerateColumns="false" DataKeyNames="WorkOrderId"  OverrideDataSourceControlSorting="true"
                                                                                    <Columns> 
                                                                                        <telerik:GridBoundColumn DataField="Description" HeaderText="Description"></telerik:GridBoundColumn> 
                                                                                        <telerik:GridBoundColumn DataField="ExpectedAmount" HeaderText="Amount" DataFormatString="{0:C}"></telerik:GridBoundColumn> 
                                                                                    </Columns> 
                                                                                </MasterTableView> 
                                                                            </telerik:RadGrid> 
                                                                        </fieldset> 
                                                                    </td> 
                                                                     <td valign="top"
                                                                        <fieldset> 
                                                                            <legend>Actual</legend> 
                                                                            <telerik:RadGrid runat="server" ID="GridActual" OnNeedDataSource="DetailsGrid_OnNeedDataSource" AllowSorting="true"
                                                                                <MasterTableView AutoGenerateColumns="false" DataKeyNames="WorkOrderId"  OverrideDataSourceControlSorting="true"
                                                                                    <Columns> 
                                                                                        <telerik:GridBoundColumn DataField="SerialNumber" HeaderText="Serial Number"></telerik:GridBoundColumn> 
                                                                                        <telerik:GridBoundColumn DataField="ReceiverNumber" HeaderText="R00#"></telerik:GridBoundColumn> 
                                                                                        <telerik:GridBoundColumn DataField="SmartCardNumber" HeaderText="S00#"></telerik:GridBoundColumn>                                                                                         
                                                                                        <telerik:GridBoundColumn DataField="Model" HeaderText="Model"></telerik:GridBoundColumn> 
                                                                                        <telerik:GridBoundColumn DataField="ActualAmount" HeaderText="Cost" DataFormatString="{0:C}"></telerik:GridBoundColumn> 
                                                                                        <%--<telerik:GridBoundColumn DataField="ExpectedAmount" HeaderText="Purchase Date"></telerik:GridBoundColumn>--%> 
                                                                                    </Columns> 
                                                                                </MasterTableView> 
                                                                            </telerik:RadGrid> 
                                                                        </fieldset> 
                                                                    </td> 
                                                                     <td valign="top"
                                                                        <fieldset> 
                                                                            <legend>Paid</legend> 
                                                                            <telerik:RadGrid runat="server" ID="GridPaid" OnNeedDataSource="DetailsGrid_OnNeedDataSource" AllowSorting="true"
                                                                                <MasterTableView AutoGenerateColumns="false" DataKeyNames="WorkOrderId"  OverrideDataSourceControlSorting="true"
                                                                                    <Columns> 
                                                                                        <telerik:GridBoundColumn DataField="Description" HeaderText="Description"></telerik:GridBoundColumn> 
                                                                                        <telerik:GridBoundColumn DataField="PaymentAmount" HeaderText="Amount" DataFormatString="{0:C}"></telerik:GridBoundColumn> 
                                                                                        <telerik:GridBoundColumn DataField="InvoiceDate" HeaderText="Date Paid" DataFormatString="{0:d}"></telerik:GridBoundColumn> 
                                                                                    </Columns> 
                                                                                </MasterTableView> 
                                                                            </telerik:RadGrid> 
                                                                        </fieldset> 
                                                                    </td> 
                                                                </tr> 
                                                            </table> 
                                                        </div> 
                                                    </asp:Panel> 
                                                </NestedViewTemplate> 
                                                <ExpandCollapseColumn visible="True"><HeaderStyle Width="20px"></HeaderStyle></ExpandCollapseColumn
                                                <PagerStyle AlwaysVisible="True" /> 
                                            </MasterTableView> 
                                            <ClientSettings AllowDragToGroup="True"></ClientSettings> 
                                        </telerik:RadGrid> 
                                    </telerik:RadPageView> 
0
Francisco Gonzalez
Top achievements
Rank 1
answered on 08 Jun 2010, 04:21 PM
I see, in the structure of RadGrid, tha you have One MasterTable with a NestedViewTemplate... and have 3 RadGrid nested...
You have use a DatailTable for better performance.. replace these Radgrid with Detailtables and populate with the event detailtable_bind
is the best choice.
0
Jessica
Top achievements
Rank 1
answered on 08 Jun 2010, 04:38 PM
That's what I had originally and the data source was still being hit 3 times.  I started using the NestedViewTemplate to customize the layout of the detail tables to meet requirements.

When I Profile the button click event to search a date range, this is the pattern I see in the calls

MasterTable Stored Procedure X 3
DetailTable 1 Stored Procedure for Item 1 X 3
DetailTable 2 Stored Procedure for Item 1 X 3
DetailTable 3 Stored Procedure for Item 1 X 3
DetailTable 1 Stored Procedure for Item 1 X 3
DetailTable 2 Stored Procedure for Item 1 X 3
DetailTable 3 Stored Procedure for Item 1 X 3
DetailTable 1 Stored Procedure for Item 2 X 3
...
DetailTable 3 Stored Procedure for Item 10 X 3 (Page Size = 10)
...
This is repeated 3 times with over 500 calls to the database.  I don't understand why the detail tables are being loaded either.  I haven't changed the LoadMode and I thought the default was to only load these when the item was expanded.  Do I need to set something explicitly to get that behavior?

I appreciate your insights, Francisco.


This is the code I use to load the detail tables:
        void GridWorkOrder_ItemCommand(object source, GridCommandEventArgs e) 
        { 
            if (e.CommandName == RadGrid.ExpandCollapseCommandName) 
            { 
                GridDataItem item = e.Item as GridDataItem; 
                if (!item.Expanded) 
                { 
                    GridNestedViewItem nestedItem = (GridNestedViewItem)item.ChildItem; 
                    string dataKeyValue = Convert.ToString(((GridDataItem)(nestedItem.ParentItem)).GetDataKeyValue("WorkOrderId")); 
                    RadGrid tempGrid = (RadGrid)nestedItem.FindControl("GridExpected"); 
                    tempGrid.Rebind(); 
 
                    tempGrid = (RadGrid)nestedItem.FindControl("GridActual"); 
                    tempGrid.Rebind(); 
 
                    tempGrid = (RadGrid)nestedItem.FindControl("GridPaid"); 
                    tempGrid.Rebind(); 
 
                } 
            } 
        } 
 
        protected void DetailsGrid_OnNeedDataSource(object source, GridNeedDataSourceEventArgs e) 
        { 
            RadGrid grid = (RadGrid)source; 
            GridNestedViewItem nestedItem = (GridNestedViewItem)grid.NamingContainer; 
            string dataKeyValue = Convert.ToString(((GridDataItem)(nestedItem.ParentItem)).GetDataKeyValue("WorkOrderId")); 
 
 
            RadGrid tempGrid = (RadGrid)nestedItem.FindControl("GridExpected"); 
            DataSourceWorkOrderDetailExpectedAmount.SelectParameters["WorkOrderId"].DefaultValue = dataKeyValue; 
            DataSourceWorkOrderDetailExpectedAmount.SelectParameters["CategoryId"].DefaultValue = TabStrip1.SelectedTab.Value; 
            tempGrid.DataSource = DataSourceWorkOrderDetailExpectedAmount; 
 
            DataSourceWorkOrdersPayment.SelectParameters["WorkOrderId"].DefaultValue = dataKeyValue; 
            DataSourceWorkOrdersPayment.SelectParameters["CategoryId"].DefaultValue = TabStrip1.SelectedTab.Value; 
            RadGrid tempGrid2 = (RadGrid)nestedItem.FindControl("GridPaid"); 
            tempGrid2.DataSource = DataSourceWorkOrdersPayment; 
 
            DataSourceWorkOrderDetailActualAmount.SelectParameters["WorkOrderId"].DefaultValue = dataKeyValue; 
            DataSourceWorkOrderDetailActualAmount.SelectParameters["CategoryId"].DefaultValue = TabStrip1.SelectedTab.Value; 
            tempGrid = (RadGrid)nestedItem.FindControl("GridActual"); 
            tempGrid.DataSource = DataSourceWorkOrderDetailActualAmount; 
        } 
0
Jessica
Top achievements
Rank 1
answered on 08 Jun 2010, 05:55 PM
The reason the entire sequence is repeated 3 times is because I am loading 3 similar grids on separate tabs.  Removing the loading of one grid caused the sequence to only be repeated 2 times.
0
Jessica
Top achievements
Rank 1
answered on 08 Jun 2010, 06:54 PM
I solved the detail table loading problem by setting visible = false by default.  When the item is expanded the event that loads them also makes them visible.

I still can't figure out why the data source is being hit 3 times.  We have another page in our project that is a simple master/detail hierarchy grid and it does not have this problem.  This grid has the DataSourceId explicitly set in the aspx and does not use the NeedDataSource event.  Could that be part of it?
0
Francisco Gonzalez
Top achievements
Rank 1
answered on 08 Jun 2010, 07:57 PM
I dont understand the reason... you have 3 radgrid inside NestedViewTemplate
This escenario i think that solved with 3 details tables... or i'm wrong?
because, i suspect that, this is reason for the need datasource event hit 3 times.

Explain me.. more about bussines flow... please.


0
Francisco Gonzalez
Top achievements
Rank 1
answered on 08 Jun 2010, 08:18 PM
I see a example that is equal...

in the Nestedviewtemplate the panel or container is visible false
<asp:Panel runat="server"> this is your....

in the example this is <asp:Panel runat="server" ID="InnerContainer" CssClass="viewWrap" Visible="false">

on the folows events set the visivility:

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.ExpandCollapseCommandName)
            {
                ((GridDataItem)e.Item).ChildItem.FindControl("InnerContainer").Visible =
                    !e.Item.Expanded;
            }
        }

        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridNestedViewItem)
            {
                e.Item.FindControl("InnerContainer").Visible = ((GridNestedViewItem)e.Item).ParentItem.Expanded;
            }
        }

0
Jessica
Top achievements
Rank 1
answered on 08 Jun 2010, 08:42 PM
I am using the NestedViewTemplate because of the flexibility it gives in designing the layout of the detail tables.  As far as I could tell, several detail tables at one level would be displayed one under the other.  I needed to display them beside each other, horizontally.

The example you gave is similar to what I did earlier today.  This solved the issue of the detail tables loading on the click event instead of just on the item expand.  I'm going to change my code to the more dynamic approach used here: 

e.Item.FindControl("InnerContainer").Visible = ((GridNestedViewItem)e.Item).ParentItem.Expanded;

Even with the NestedViewTemplate commented out the data source is still called three times for the master table so there must be some other reason for this.  I am seeing the same behavior when the detail tables are bound.
Tags
Grid
Asked by
Jessica
Top achievements
Rank 1
Answers by
Francisco Gonzalez
Top achievements
Rank 1
Jessica
Top achievements
Rank 1
Share this question
or