RadGrid sort all DetailTables when one is sorted

1 posts, 0 answers
  1. Attila Antal
    Attila Antal avatar
    6 posts
    Member since:
    Sep 2017

    Posted 10 Apr Link to this post

    Requirements

    Telerik Product and Version used

    UI for ASP.NET AJAX 2019 R1

    Supported Browsers and Platforms

    all browsers supported by Telerik UI for ASP.NET AJAX suite

    Components/Widgets used (JS frameworks, etc.)

    RadGrid, .NET 4.0/4.5 C#, VB

    PROJECT DESCRIPTION

    By default DetailTables are rendered as separate tables for each grid row and they act independently from the others. This Code Library sample shows the way to make all DetailTables sort at once when one is being sorted by the user.

    DEMO






    RadGrid Markup

    <telerik:RadGrid ID="RadGrid1" runat="server" Skin="Default" EnableHierarchyExpandAll="true"
        OnDetailTableDataBind="RadGrid1_DetailTableDataBind"
        OnSortCommand="RadGrid1_SortCommand"
        OnNeedDataSource="RadGrid1_NeedDataSource">
        <MasterTableView DataKeyNames="OrderID" ShowHeader="true" AutoGenerateColumns="false" Name="MasterTable" AllowSorting="true" HierarchyDefaultExpanded="true" RetainExpandStateOnRebind="true">
            <DetailTables>
                <telerik:GridTableView DataKeyNames="OrderID" AutoGenerateColumns="true" Name="ChildTable" AllowSorting="true"></telerik:GridTableView>
            </DetailTables>
            <Columns>
                <telerik:GridBoundColumn DataField="OrderID" DataType="System.Int32"
                    FilterControlAltText="Filter OrderID column" HeaderText="MasterTable Records"
                    ReadOnly="True" SortExpression="OrderID" UniqueName="OrderID">
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C# - Code behind

    protected void RadGrid1_DetailTableDataBind(object sender, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
    {
        GridDataItem parentItem = (GridDataItem)((GridNestedViewItem)e.DetailTableView.NamingContainer).ParentItem;
        int OrderID = (int)parentItem.GetDataKeyValue("OrderID");
        e.DetailTableView.DataSource = OrdersTable().Select(string.Format("ShipName LIKE '%{0}%'", OrderID));
    }
     
     
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        if (!e.IsFromDetailTable)
            RadGrid1.DataSource = OrdersTable().Select().Take(3);
    }
     
     
    private DataTable OrdersTable()
    {
        DataTable dt = new DataTable();
     
        dt.Columns.Add(new DataColumn("OrderID", typeof(int)));
        dt.Columns.Add(new DataColumn("OrderDate", typeof(DateTime)));
        dt.Columns.Add(new DataColumn("Freight", typeof(decimal)));
        dt.Columns.Add(new DataColumn("ShipName", typeof(string)));
        dt.Columns.Add(new DataColumn("ShipCountry", typeof(string)));
     
        dt.PrimaryKey = new DataColumn[] { dt.Columns["OrderID"] };
     
        for (int i = 0; i < 13; i++)
        {
            int index = i + 1;
     
            DataRow row = dt.NewRow();
     
            row["OrderID"] = index;
            row["OrderDate"] = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0).AddHours(index);
            row["Freight"] = index * 0.1 + index * 0.01;
            row["ShipName"] = "Name " + index;
            row["ShipCountry"] = "Country " + index;
     
            dt.Rows.Add(row);
        }
     
        return dt;
    }
     
     
    protected void RadGrid1_SortCommand(object sender, GridSortCommandEventArgs e)
    {
        // check whether the sorting comes from a childtable
        if (e.Item.OwnerTableView.Name == "ChildTable")
        {
            // cancel original sort command
            e.Canceled = true;
            // field name to be sorted
            string fieldName = e.SortExpression;
            // new sort order
            GridSortOrder newSortOrder = e.NewSortOrder;
            // loop through the RadGrid's items
            foreach (GridDataItem dataItem in RadGrid1.Items)
            {
                // check if the dataitem has child tables
                if (dataItem.HasChildItems)
                {
                    // get reference to the detailtable
                    GridTableView childtable = dataItem.ChildItem.NestedTableViews.First() as GridTableView;
                    // apply new sorting for the table, where you can re-use the fieldname and sortorder from earlier
                    childtable.SortExpressions.AddSortExpression(new GridSortExpression() { FieldName = fieldName, SortOrder = newSortOrder });
                    // finally, rebind the detailtable to refresh their content
                    childtable.Rebind();
                }
            }
        }
    }


    VB - Code behind

    Protected Sub RadGrid1_DetailTableDataBind(sender As Object, e As Telerik.Web.UI.GridDetailTableDataBindEventArgs)
        Dim parentItem As GridDataItem = DirectCast(DirectCast(e.DetailTableView.NamingContainer, GridNestedViewItem).ParentItem, GridDataItem)
        Dim OrderID As Integer = DirectCast(parentItem.GetDataKeyValue("OrderID"), Integer)
        e.DetailTableView.DataSource = OrdersTable().Select(String.Format("ShipName LIKE '%{0}%'", OrderID))
    End Sub
     
     
    Protected Sub RadGrid1_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs)
        If Not e.IsFromDetailTable Then
            RadGrid1.DataSource = OrdersTable().Select().Take(3)
        End If
    End Sub
     
    Private Function OrdersTable() As DataTable
        Dim dt As New DataTable()
     
        dt.Columns.Add(New DataColumn("OrderID", Type.GetType("System.Int32")))
        dt.Columns.Add(New DataColumn("OrderDate", Type.GetType("System.DateTime")))
        dt.Columns.Add(New DataColumn("Freight", Type.GetType("System.Decimal")))
        dt.Columns.Add(New DataColumn("ShipName", Type.GetType("System.String")))
        dt.Columns.Add(New DataColumn("ShipCountry", Type.GetType("System.String")))
     
        Dim PrimaryKeyColumns As DataColumn() = New DataColumn(0) {}
        PrimaryKeyColumns(0) = dt.Columns("OrderID")
        dt.PrimaryKey = PrimaryKeyColumns
     
        For i As Integer = 0 To 13 - 1
            Dim index As Integer = i + 1
            Dim row As DataRow = dt.NewRow()
     
            row("OrderID") = index
            row("OrderDate") = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0).AddHours(index)
            row("Freight") = index * 0.1 + index * 0.01
            row("ShipName") = "Name " & index
            row("ShipCountry") = "Country " & index
     
            dt.Rows.Add(row)
        Next
     
        Return dt
    End Function
     
    Protected Sub RadGrid1_SortCommand(sender As Object, e As GridSortCommandEventArgs)
        'check whether the sorting comes from a childtable
        If e.Item.OwnerTableView.Name = "ChildTable" Then
            'cancel original sort command
            e.Canceled = True
            'field name to be sorted
            Dim fieldName As String = e.SortExpression
            'new sort order
            Dim newSortOrder As GridSortOrder = e.NewSortOrder
            'loop through the RadGrid's items
            For Each dataItem As GridDataItem In RadGrid1.Items
                'check if the dataitem has child tables
                If dataItem.HasChildItems Then
                    'get reference to the detailtable
                    Dim childtable As GridTableView = dataItem.ChildItem.NestedTableViews(0)
                    'apply new sorting for the table, where you can re-use the fieldname and sortorder from earlier
                    childtable.SortExpressions.AddSortExpression(New GridSortExpression() With {.FieldName = fieldName, .SortOrder = newSortOrder})
                    'finally, rebind the detailtable to refresh their content
                    childtable.Rebind()
                End If
            Next
        End If
    End Sub
Back to Top