Monday, October 21, 2013

Telerik RadGrid : Add Sorting to RadGrids EASILY !!!

Hi all, This tutorial will explain how to add sorting for a RadGrid easily. Once you have added the grid to your page, you'll have to add the following codes according to your grid.

First in the aspx or ascx page or controller,

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
    <script type="text/javascript">
   
        function SortColumn(ColumnName)
        {
         var masterTable = $find("<%= Grid1.ClientID %>").get_masterTableView();
          masterTable.sort(ColumnName);
        }
   
    </script>

</telerik:RadCodeBlock>

Then in the code behind, add the following code. 

protected void Grid1_PreRender(object sender, EventArgse)
        {           

            foreach (GridColumncol in Grid1.MasterTableView.RenderColumns)
            {
                foreach (GridHeaderItemheader in Grid1.MasterTableView.GetItems(GridItemType.Header))
                {
                    header[col.UniqueName].Attributes.Add("OnClick", "return SortColumn('" + col.UniqueName + "');");
                }
            }
        }



Hope this will help someone... Thanks :-)





No comments:

Post a Comment