Wednesday, May 5, 2010

Telerik RAD Grid nested table view items

In my current project, we are using the Telerik RadControls for ASP.NET Ajax very heavily, specially the Telerik’s RadGrid.
I liked the product, the ease to implement and clean Ajaxification of all kind of server controls is cool.
However, while working with Telerik’s RadGrid a came across a couple of problems. Thanks to the Telerik support team those issues were answered and taken care of. But, before contacting them, I spend considerable time and effort trying to get a solution without success. So here is the information hidden deep in Telerik’s documentation.

We were trying to implement custom/programmatic  sorting, filtering  and grouping on Telerik’s RadGrid.
To do that we would need to grab the specific TableView (master or nested) and use the GetItems() method passing the Grid Item type specifying what you want.

First step - Grabbing the table view:
The following doesn’t work if you need to pull out the  FilteringItem out of a particular nested table view:

MyRadGrid.MasterTableView.DetailTables[0]

Here’s a generic method that I created and placed in the application specific Page base class:
protected GridTableView FindGridTableView(RadGrid radGrid, string tableViewName) {

    GridTableView nestedTableView = null;

    foreach (GridDataItem item in radGrid.Items) {

        if (item.OwnerTableView.Name == tableViewName) {
            nestedTableView = item.OwnerTableView;
            break;
        }
    }

    return nestedTableView;
}

Pretty self explanatory, given the grid instance and the table view name that you intend to find, this method will find and return irrespective of the nesting level.

Second step – Grabbing Grid Item through the table view:
And, this is how you get the filter or command items out of the nested table we just retrieved:

//using Telerik.Web.UI;
    
    //grab the Filtering Item
    GridFilteringItem filterAttr = (GridFilteringItem)tableView.GetItems(GridItemType.FilteringItem)[0];
    //file the filter command for a particular column 
    //(not going into how to set column values as you can get it pretty easily on telerik's site)
    filterAttr.FireCommandEvent("Filter", new Pair(column.CurrentFilterFunction.ToString(), column.UniqueName));
    
    //grabbing the command item
    GridCommandItem cmdItem = (GridCommandItem)tableView.GetItems(GridItemType.CommandItem)[0];
    //using Command Item
    (cmdItem.FindControl("MyDropDownList") as DropDownList).SelectedIndex = 0;
    
    // etc etc etc ...
The version of Telerik I used was - 2009.2.826.35.

On a side note, I would strongly recommend using ASP.NET MVC framework if you are looking to create highly ajaxified applications. If not MVC, the other way to go will be using Web Forms with WCF on the server with WebHttpBinding to make use of script and json support. This way you can make asynchronous calls to the service via java script surpassing the page life cycle when required…. but why use Web Forms if you you intend to surpass the page life cycle??? MVC is a very powerful framework made up all the goodies that you can imagine for today’s web application development.
Contact me if you want me to to explain this to you personally with a Demo.

1 comment: