<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DetailTablesAggregates.aspx.cs" Inherits="pages_Grid_Hierarchy_DetailTableAggregates" %>

<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> 
        <telerik:RadGrid ID="RadGrid1" Skin="Office2007" ShowGroupPanel="True" ShowStatusBar="true"
            runat="server" DataSourceID="SqlDataSource1" Width="95%" AutoGenerateColumns="False"
            PageSize="3" AllowSorting="True" AllowMultiRowSelection="False" AllowPaging="True" GridLines="None" ShowFooter="true">
            <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
            <MasterTableView Width="100%" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID" AllowMultiColumnSorting="True" AdditionalDataFieldNames>
                <DetailTables>
                    <telerik:GridTableView DataKeyNames="OrderID" DataSourceID="SqlDataSource2" Width="100%" runat="server">
                        <ParentTableRelation>
                            <telerik:GridRelationFields DetailKeyField="CustomerID" MasterKeyField="CustomerID" />
                        </ParentTableRelation>
                        <DetailTables>
                            <telerik:GridTableView DataKeyNames="OrderID" DataSourceID="SqlDataSource3" Width="100%" runat="server">
                                <ParentTableRelation>
                                    <telerik:GridRelationFields DetailKeyField="OrderID" MasterKeyField="OrderID" />
                                </ParentTableRelation>
                                <Columns>
                                    <telerik:GridBoundColumn SortExpression="UnitPrice" HeaderText="Unit Price" DataField="UnitPrice" UniqueName="UnitPrice" Aggregate="Max">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn SortExpression="Quantity" HeaderText="Quantity" DataField="Quantity" UniqueName="Quantity" Aggregate="Sum" AllowFiltering="False">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn SortExpression="Discount" HeaderText="Discount" DataField="Discount" UniqueName="Discount" Aggregate="Min">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridCalculatedColumn DataFormatString="{0:c}" HeaderText="Total Price" UniqueName="TotalPrice"
                                        DataType="System.Double" DataFields="UnitPrice, Quantity" Expression="{0}*{1}"
                                        FooterAggregateFormatString="Total: {0:c}" Aggregate="Sum" />
                                </Columns>
                                <GroupByExpressions>
                                    <telerik:GridGroupByExpression>
                                        <SelectFields>
                                            <telerik:GridGroupByField FieldAlias="Quantity" FieldName="Quantity" FormatString="" HeaderText=""></telerik:GridGroupByField>
                                        </SelectFields>
                                        <GroupByFields>
                                            <telerik:GridGroupByField FieldAlias="Quantity" FieldName="Quantity" FormatString="" HeaderText=""></telerik:GridGroupByField>
                                        </GroupByFields>
                                    </telerik:GridGroupByExpression>
                                </GroupByExpressions>
                                <SortExpressions>
                                    <telerik:GridSortExpression FieldName="Quantity" SortOrder="Descending"></telerik:GridSortExpression>
                                </SortExpressions>
                            </telerik:GridTableView>
                        </DetailTables>
                        <Columns>
                            <telerik:GridBoundColumn SortExpression="OrderID" HeaderText="OrderID" DataField="OrderID" UniqueName="OrderID" Aggregate="Count" ReadOnly="True">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn SortExpression="OrderDate" HeaderText="Date Ordered" DataField="OrderDate" UniqueName="OrderDate" Aggregate="First" AllowFiltering="False">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn SortExpression="EmployeeID" HeaderText="EmployeeID" DataField="EmployeeID" UniqueName="EmployeeID">
                            </telerik:GridBoundColumn>
                        </Columns>
                        <SortExpressions>
                            <telerik:GridSortExpression FieldName="OrderDate"></telerik:GridSortExpression>
                        </SortExpressions>
                    </telerik:GridTableView>
                </DetailTables>
                <Columns>
                    <telerik:GridBoundColumn SortExpression="CustomerID" HeaderText="CustomerID" DataField="CustomerID" UniqueName="CustomerID" Aggregate="Count">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn SortExpression="ContactName" HeaderText="Contact Name" DataField="ContactName" UniqueName="ContactName" Aggregate="First">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn SortExpression="CompanyName" HeaderText="Company" DataField="CompanyName" UniqueName="CompanyName" Aggregate="Last" AllowFiltering="False">
                    </telerik:GridBoundColumn>
                </Columns>
                <SortExpressions>
                    <telerik:GridSortExpression FieldName="CompanyName"></telerik:GridSortExpression>
                </SortExpressions>
            </MasterTableView>
            <ClientSettings ReorderColumnsOnClient="True" AllowDragToGroup="True" AllowColumnsReorder="True">
                <Resizing AllowRowResize="True" AllowColumnResize="True"></Resizing>
            </ClientSettings>
        </telerik:RadGrid>
        <br />
        <br />
         <asp:SqlDataSource ID="SqlDataSource1" 
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            ProviderName="System.Data.SqlClient"
            SelectCommand="SELECT * FROM Customers" runat="server"></asp:SqlDataSource>
         <asp:SqlDataSource ID="SqlDataSource2" 
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            ProviderName="System.Data.SqlClient" 
            SelectCommand="SELECT * FROM Orders Where CustomerID = @CustomerID" runat="server">
            <SelectParameters>
                <asp:SessionParameter Name="CustomerID" SessionField="CustomerID" Type="string" />
            </SelectParameters>
        </asp:SqlDataSource>
         <asp:SqlDataSource ID="SqlDataSource3" 
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            ProviderName="System.Data.SqlClient"
            SelectCommand="SELECT * FROM [Order Details] where OrderID = @OrderID" runat="server">
            <SelectParameters>
                <asp:SessionParameter Name="OrderID" SessionField="OrderID" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>    
    </div>
    </form>
</body>
</html>
