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

Hiding RadGrid detail table columns during PreRender

4 Answers 495 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 06 May 2010, 11:58 PM
I am trying to remove the editing interfaces when a user is not allowed to edit the RadGrid contents. The grid has a master table view with 2 detail tables at the same level. In the grid's PreRender event, I use the following code to hide the edit and delete columns, but only the Master View's columns are hidden when the page is first rendered. Clicking the master grid's Refresh Data button then hides the detail tables' columns even if this code is prevented from re-running, so it seems the setting has been made, but not used during the first render. I have similar code in the ItemCreated event which sets the GridCommandItem display to None. That also displays correctly for the master table on the initial page render, but only takes affect for the detail tables after the Refresh Data. How can I hide the columns and command items during the first page rendering. I am using Telerik version 2010.1.415.35.

protected void RadGridSubjectArea_PreRender(object sender, EventArgs e)  
    {  
        Trace.Write("Event", this.ToString() + ": " + "RadGridSubjectArea_PreRender");  
        RadGrid grid = (RadGrid)sender;  
 
        bool editAllowed = this.isEditAllowed;  
        grid.AllowAutomaticInserts = editAllowed;   //Added safety since controls should be hidden  
        grid.AllowAutomaticDeletes = editAllowed;  
        grid.AllowAutomaticUpdates = editAllowed;  
 
        GridTableView view = grid.MasterTableView;  //Top-level view  
        //Set editability of this view and child views at any level  
        this.GridTableHierarchySetEditability(view, editAllowed);  
 
        //Try to force the display to refresh?
        //Clicking master grid's Refresh Data hides the columns even if this code is NOT re-run,
        // so it seems the setting has been made, but not used during the first render?
        //grid.Rebind();    //This prevents clicking Refresh Data from working.  
    }  
 
    protected void GridTableHierarchySetEditability(GridTableView view, bool editAllowed)  
    {  
        Trace.Write("Event", this.ToString() + ": " + "GridTableHierarchySetEditability");  
        //Recursively set editability for this table view and all its children.  
        //Unnecessary since we hide command buttons, but this is further protection.  
        view.AllowAutomaticInserts = editAllowed;  
        view.AllowAutomaticDeletes = editAllowed;  
        view.AllowAutomaticUpdates = editAllowed;  
 
        //Show or hide command columns (generally edit or delete)  
        foreach (GridColumn col in view.Columns)  
        {  
            if (col.ColumnType == "GridEditCommandColumn" || col.ColumnType == "GridButtonColumn")  
            {  
                col.Visible = editAllowed;  
            }  
        }  
 
        //Process any child tables  
        foreach (GridTableView child in view.DetailTables)  
        {  
            this.GridTableHierarchySetEditability(child, editAllowed);  
        }  
    }  
 
<BR> 

 


 

 

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 07 May 2010, 06:46 AM
Hello Paul,

I guess you are using HierarchyLoadMode as "Client" in your grid. If so, try rebinding the grid explicitly in PreRender event in order to achieve this. Hope this information helps you.

C#:
 
 protected
 void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        //   your code

        grid.MasterTableView.Rebind(); 
    } 

Regards,
Shinu.


0
Paul
Top achievements
Rank 1
answered on 07 May 2010, 11:31 AM
No, I had HierarchyLoadMode="ServerOnDemand" and HierarchyDefaultExpanded="true". I had tried including grid.ReBind() in the PreRender event handler, but that did not solve the problem, and instead left the Visible=false columns still visible even after clicking the RefreshData button. I tried all 3 hierarchy load modes, with the same result. I tried both grid.Rebind() and grid.MasterTableView.ReBind(), and the result was always the same, as I described above. I also tried HierarchyDefaultExpanded=false, with the same failed result. Again, adding the ReBind() method in the PreRender caused the invisble columns to remain displayed after Refresh Data. I tried adding an OnPreRender event handler to the detail tables, to set the columns to visible=false, but that event never seems to be generated.
0
Veli
Telerik team
answered on 13 May 2010, 09:54 AM
Hi Paul,

Thank you for the details. Try the following approach instead:

protected void GridTableHierarchySetEditability(GridTableView view, bool editAllowed)
{
    foreach (GridColumn col in view.Columns)
    {
        if (col is GridEditCommandColumn || col is GridButtonColumn)
        {
            col.Visible = editAllowed;
        }
 
        foreach (GridDataItem item in view.Items)
        {
            foreach (GridTableView innerView in item.ChildItem.NestedTableViews)
            {
                GridTableHierarchySetEditability(innerView, editAllowed);
            }
        }
    }
}

The above code finds all nested tables from the data items instead of the nested table. This is because the GridTableView.DetailTables collection is only a set of templates for creating the nested tables. The real detail tables are contained in the GridDataItem.ChildItem.NestedTableViews collection.


Greetings,
Veli
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Paul
Top achievements
Rank 1
answered on 13 May 2010, 12:17 PM
Thank you for the solution and the explanation. It makes sense now. The suggested code needed some minor updating to work. Here is the final routine.

    protected void GridTableHierarchySetEditability(GridTableView view, bool editAllowed)  
    {  
        //Recursively set editability for this table view and all its children.  
 
        //This should be unnecessary since we hide command buttons, but is further protection.  
        view.AllowAutomaticInserts = editAllowed;  
        view.AllowAutomaticDeletes = editAllowed;  
        view.AllowAutomaticUpdates = editAllowed;  
 
        //If editing is not allowed, remove any edit or command-button columns.  
        if (!editAllowed)  
        {  
            //Remove editing columns in the current view  
            foreach (GridColumn col in view.Columns)  
            {  
                if (col is GridEditCommandColumn || col is GridButtonColumn)  
                {  
                    col.Visible = editAllowed;  
                }  
            }  
        }  
 
        //Propagate the editability changes to the child views of each item in this view.  
        //Telerik's support notes:  
        //This code finds all nested tables from the data items instead of the nested table.   
        //This is because the GridTableView.DetailTables collection is only a set of templates   
        //for creating the nested tables. The real detail tables are contained   
        //in the GridDataItem.ChildItem.NestedTableViews collection.  
        if (view.HasDetailTables)  
        {  
            foreach (GridDataItem item in view.Items)  
            {  
                if (item.HasChildItems)  
                {  
                    foreach (GridTableView innerView in item.ChildItem.NestedTableViews)  
                    {  
                        GridTableHierarchySetEditability(innerView, editAllowed);  
                    }  
                }  
            }  
        }  
    }  
 
Tags
Grid
Asked by
Paul
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Paul
Top achievements
Rank 1
Veli
Telerik team
Share this question
or