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

How to rebind only detail table

10 Answers 1090 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kent Williams
Top achievements
Rank 1
Kent Williams asked on 07 Aug 2009, 11:09 PM
I have a grid that contains a detail table for each record. Inside the detail table I have two commands. I handle the 

ItemCommand event, in which I execute a stored procedure depending on the command name and argument. So far so good. The issue is I can't seem to find a way to rebind the detail table, so when the page refreshes for the user they see the results. I have tried various things, but none seem to work. If I click refresh on the detail table then the detail table collapses. (Note: I need to try as hard as possible to do this via a stored procedure - there is auditing code, etc. inside of it).

Any help would be greatly appriciated.

Thanks,
Kent

        switch (e.CommandName)  
        {  
            case "terminate" :  
                db = new CasprDataContext();  
                mgrRoleID = int.Parse(e.CommandArgument.ToString());  
 
                //TEST ONE:  
                //mgrRole = db.EICPMgrRoles.Single(mr => mr.MgrRoleGID == mgrRoleID);  
                //mgrRole.RoleEndDate = DateTime.Now;  
                //db.SubmitChanges();  
 
                //TEST TWO (preferred):  
                //execute stored procedure  
                db.sp_UpdateManagerRole_End(mgrRoleID);  
 
                //TEST TWO-A:  
                //Rebind the details table  
                //dataItem.DataBind();  
 
                //TEST TWO-B:  
                managersGrid.MasterTableView.DetailTables[0].Rebind();  
                break;  
            case "restore" :  
                db = new CasprDataContext();  
                mgrRoleID = int.Parse(e.CommandArgument.ToString());  
 
                //TEST ONE:  
                //mgrRole = db.EICPMgrRoles.Single(mr => mr.MgrRoleGID == mgrRoleID);  
                //mgrRole.RoleEndDate = null;  
                //db.SubmitChanges();  
 
                //TEST TWO (preferred):  
                //execute stored procedure  
                db.sp_UpdateManagerRole_Restore(mgrRoleID);  
 
                //TEST TWO-A:  
                //Rebind the details table  
                //dataItem.DataBind();  
 
                //TEST TWO-B:  
                managersGrid.MasterTableView.DetailTables[0].Rebind();  
                break;  
        } 

<DetailTables> 
            <telerik:GridTableView DataKeyNames="MgrFID" DataSourceID="managerRolesDataSource" AllowFilteringByColumn="false" CommandItemDisplay="Bottom" EditFormSettings-ColumnNumber="2">  
                <ParentTableRelation> 
                    <telerik:GridRelationFields MasterKeyField="MgrGID" DetailKeyField="MgrFID" /> 
                </ParentTableRelation> 
                 <Columns> 
                    <telerik:GridBoundColumn DataField="MgrRoleGID" DataType="System.Int32"  ReadOnly="true" Visible="false" 
                        HeaderText="MgrRoleGID" SortExpression="MgrRoleGID" UniqueName="MgrRoleGID">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="MgrFID" DataType="System.Int32" Visible="false"   
                        HeaderText="MgrFID" ReadOnly="True" SortExpression="MgrFID" UniqueName="MgrFID">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridDropDownColumn DataField="RoleFID" DataType="System.Int32"   
                        DataSourceID="rolesDataSource" ListTextField="RoleDesc" ListValueField="RoleGID" 
                        HeaderText="Role" SortExpression="RoleFID"   
                        UniqueName="RoleFID">  
                        </telerik:GridDropDownColumn> 
                    <telerik:GridDropDownColumn DataField="RegionFID" DataType="System.Int32" EditFormColumnIndex="1" 
                        DataSourceID="regionsDataSource" ListTextField="RegionDesc" ListValueField="RegionGID" 
                        HeaderText="Region" SortExpression="RegionFID"   
                        UniqueName="RegionFID">  
                    </telerik:GridDropDownColumn> 
                    <telerik:GridBoundColumn DataField="RoleStartDate" DataType="System.DateTime"   
                        HeaderText="Start Date" SortExpression="RoleStartDate" ReadOnly="true" 
                        UniqueName="RoleStartDate">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="RoleEndDate" DataType="System.DateTime"  ReadOnly="true" 
                        HeaderText="End Date" SortExpression="RoleEndDate" UniqueName="RoleEndDate">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridTemplateColumn UniqueName="actions">  
                        <ItemTemplate> 
                            <asp:LinkButton runat="server" CommandName="terminate" CommandArgument='<%# Eval("MgrRoleGID") %>' Visible='<%# Eval("RoleEndDate") == null ? true : false %>'>Terminate</asp:LinkButton> 
                            <asp:LinkButton runat="server" CommandName="restore" CommandArgument='<%# Eval("MgrRoleGID") %>' Visible='<%# Eval("RoleEndDate") == null ? false : true %>'>Restore</asp:LinkButton> 
                        </ItemTemplate> 
                    </telerik:GridTemplateColumn> 
                </Columns> 
            </telerik:GridTableView> 
        </DetailTables> 

<asp:LinqDataSource ID="managersDataSource" runat="server"   
    ContextTypeName="Caspr.CasprDataContext" OrderBy="MgrLastName, MgrFirstName"   
    TableName="EICPMgrs" EnableInsert="True" EnableUpdate="True">  
</asp:LinqDataSource> 
<asp:LinqDataSource ID="managerRolesDataSource" runat="server"   
    ContextTypeName="Caspr.CasprDataContext" TableName="EICPMgrRoles"   
    Where="MgrFID == @MgrFID" EnableInsert="True"   
    oninserting="managerRolesDataSource_Inserting">  
    <WhereParameters> 
        <asp:SessionParameter Name="MgrFID" Type="Int32" SessionField="MgrFID" DefaultValue="0" /> 
    </WhereParameters> 
</asp:LinqDataSource> 
<asp:LinqDataSource ID="rolesDataSource" runat="server"   
    ContextTypeName="Caspr.CasprDataContext" OrderBy="RoleDesc"   
    Select="new (RoleDesc, RoleGID)" TableName="EICPRoles">  
</asp:LinqDataSource> 
<asp:LinqDataSource ID="regionsDataSource" runat="server"   
    ContextTypeName="Caspr.CasprDataContext" OrderBy="RegionDesc"   
    Select="new (RegionGID, RegionDesc)" TableName="EICPRegions">  
</asp:LinqDataSource> 
 

10 Answers, 1 is accepted

Sort by
0
Accepted
Tsvetoslav
Telerik team
answered on 12 Aug 2009, 08:55 AM
Hello Kent,

You can rebind the given detail table view through the following line of code in the ItemCommand event:

e.Item.OwnerTableView.Rebind(); 

I hope this helps.

Regards,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Kent Williams
Top achievements
Rank 1
answered on 12 Aug 2009, 03:01 PM
Works like a charm! Thank you!
0
Polaris431
Top achievements
Rank 1
answered on 04 Sep 2009, 08:51 AM
I have a similiar problem. My hierarchical grid uses a popup window for editing. I am using the design scenario as posted at:
http://demos.telerik.com/aspnet-ajax/controls/examples/integration/gridandwindow/defaultcs.aspx?product=grid

When the user inserts a new record on the popup window, javascript calls the parent browser and through the AjaxManager a refresh is done on the grid. Refreshing the grid however causes the expanded detail table to collapse. What I want is that the detail table remains expanded and the new recorded gets added to it. I have tried adding:

this.RadGrid1.MasterTableView.DetailTables[0].Rebind();

in the AjaxRequest event but this does nothing. The DetailTableDataBind event never gets called.

The suggestion you made above:

e.Item.OwnerTableView.Rebind();

is contingent upon the ItemCommand getting executed, but that will not get executed when I am trying to refresh the details table through the AjaxRequest event.


Johann


0
Polaris431
Top achievements
Rank 1
answered on 04 Sep 2009, 09:07 AM
I came across the following posting:
http://www.telerik.com/community/code-library/aspnet-ajax/grid/retain-expanded-selected-state-in-hierarchy-on-rebind.aspx

After reading this, it seems that it is not possible to just refresh the details table while retaining the expanded state. From what I understand, the parent table needs to be rebound and the details table needs to be expanded again.
0
Polaris431
Top achievements
Rank 1
answered on 04 Sep 2009, 09:33 AM
This does raise the question though as to why Telerik even has the following:

this.RadGrid1.MasterTableView.DetailTables[0].Rebind();

If rebinding a details table has no affect on the UI, what's the point of even having this Rebind method?
0
Sebastian
Telerik team
answered on 04 Sep 2009, 10:06 AM
Hello Polaris431,

I think that the information from the Note: section of this help article explains the nature of the DetailTables collection of the tables in the hierarchy and when it can be used. If you want to rebind a particular table in the hierarchy, you will need to invoke the Rebind() method for its very instance.

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Lucania
Top achievements
Rank 1
answered on 20 Jul 2011, 01:14 AM
I know this is an old thread, and even the link provided above seems to have disappeared, but I have found that RadGrid1.MasterTableView.DetailTables[0].Rebind(); works only for items whose Expanded property is false. If you iterate through the MasterTableView.Items, and save a list of the expanded ones, and set Expanded to false, then call RadGrid1.MasterTableView.DetailTables[0].Rebind(); then iterate through the saved list setting Expanded to true, it refreshed all the detail tables, and leaves them expanded as they were.

Hope this helps someone.

Edit: As an enhancement, would it be possible for the Rebind() function to include this code? The code I use is:
protected void RefreshDetailTable(RadGrid rg, int iDetailIndex)
{
    // Refresh detail table by setting Expanded to false for all, then setting it to true again
    List<GridEditableItem> eiExpanded = new List<GridEditableItem>();
    foreach (GridItem item in rg.MasterTableView.Items)
    {
        if (item is GridEditableItem)
        {
            GridEditableItem ei = item as GridEditableItem;
            if (ei.Expanded)
            {
                eiExpanded.Add(ei);
                ei.Expanded = false;
            }
        }
    }
    rg.MasterTableView.DetailTables[iDetailIndex].Rebind();
    foreach (GridEditableItem ei in eiExpanded)
    {
        ei.Expanded = true;
    }
}


ROSCO
Andrea
Top achievements
Rank 1
commented on 08 May 2021, 10:56 AM

Rosco you saved my day. Thank you
0
Sebastian
Telerik team
answered on 20 Jul 2011, 09:47 AM
Hi Ross,

I think that the solutions from this code library entry presents an universal solution about how to retain the expanded state of items in grid hierarchy on rebind. Review it and resort on it if applicable for your scenario.

I updated the previous link to point to the new location of the help topic - thanks for pointing the discrepancy.

Best regards,
Sebastian
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Lucania
Top achievements
Rank 1
answered on 21 Jul 2011, 12:27 AM
Thanks Sebastian, that appears to be a much more generic solution, I will investigate if it's better for my needs. Is there any reason code like this isn't included in the library? It would be really nice if RadGrid and GridTableView had boolean RetainSelectedOnRebind and RetainExpandedOnRebind properties (or similar).

Thanks again

ROSCO
0
Sebastian
Telerik team
answered on 21 Jul 2011, 09:14 AM
Hello Ross,

Well, this is rather an extended solution which spans outside of the grid's general functionality, that is the reason it has not been integrated as part of its internal code. Nevertheless, I will pass your proposal to our developers to be contemplated further.

Best regards,
Sebastian
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
Grid
Asked by
Kent Williams
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Kent Williams
Top achievements
Rank 1
Polaris431
Top achievements
Rank 1
Sebastian
Telerik team
Lucania
Top achievements
Rank 1
Share this question
or