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

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <telerik:RadAjaxManager ID="RadAjaxManager1" EnableAJAX="false" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">

            function PrintRadGrid() {
                            
                var sh = '<%= Page.ClientScript.GetWebResourceUrl(gvIndents.GetType(),String.Format("Telerik.Web.UI.Skins.{0}.Grid.{0}.css",gvIndents.Skin)) %>';
                var styleStr = "<html><head><link href = '" + sh + "' rel='stylesheet' type='text/css'></link></head>";
                var grid = $find('<%= gvIndents.ClientID %>');
                var MasterTable = $find('<%= gvIndents.ClientID %>').get_masterTableView();
                var selectedRows = MasterTable.get_selectedItems();
                var htmlcontent = "<table>";
                var headerRow = $telerik.$(grid.get_masterTableView().get_element()).find("th.rgHeader").parent("tr")[0];
                htmlcontent = htmlcontent + "<tr>" + headerRow.innerHTML + "</tr>";

                for (var i = 0; i < selectedRows.length; i++) {
                    var row = selectedRows[i];
                    htmlcontent = htmlcontent + "<tr>" + row.get_element().innerHTML + "</tr>";
                    if (row.get_nestedViews().length > 0) {
                        var detailTableView = row.get_nestedViews()[0];
                        headerRow = $telerik.$(detailTableView.get_element()).find("th.rgHeader").parent("tr")[0];
                        htmlcontent = htmlcontent + "<tr>" + headerRow.innerHTML + "</tr>";

                        var nestedSelectedRows = row.get_nestedViews()[0].get_selectedItems();
                        for (var j = 0; j < nestedSelectedRows.length; j++) {
                            var nestedRow = nestedSelectedRows[j];
                            htmlcontent = htmlcontent + "<tr>" + nestedRow.get_element().innerHTML + "</tr>";
                        }
                    }
                }


                htmlcontent = styleStr + "<body><div class='RadGrid RadGrid_Simple'>" + htmlcontent + "</table></div></body></html>";

                var previewWnd = window.open('about:blank', '', '', false);
                previewWnd.document.open();
                previewWnd.document.write(htmlcontent);
                previewWnd.document.close();
                previewWnd.print();
            }
        </script>
    </telerik:RadCodeBlock>
    <telerik:RadGrid ID="gvIndents" AllowPaging="true" AllowMultiRowSelection="true" runat="server" DataSourceID="SqlDataSource1">
        <MasterTableView AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1">
            <Columns>
                <telerik:GridBoundColumn DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True"
                    SortExpression="CustomerID" UniqueName="CustomerID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName"
                    UniqueName="CompanyName">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName"
                    UniqueName="ContactName">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Country" HeaderText="Country" SortExpression="Country"
                    UniqueName="Country">
                </telerik:GridBoundColumn>
            </Columns>
            <DetailTables>
                <telerik:GridTableView Visible="true" DataSourceID="SqlDataSource2" DataKeyNames="OrderID"
                    AutoGenerateColumns="false">
                    <ParentTableRelation>
                        <telerik:GridRelationFields DetailKeyField="CustomerID" MasterKeyField="CustomerID" />
                    </ParentTableRelation>
                    <Columns>
                        <telerik:GridBoundColumn DataField="OrderID" HeaderText="OrderID" ReadOnly="True"
                            SortExpression="OrderID" UniqueName="OrderID">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID"
                            UniqueName="CustomerID">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate"
                            UniqueName="OrderDate">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ShipCountry" HeaderText="ShipCountry" SortExpression="ShipCountry"
                            UniqueName="ShipCountry">
                        </telerik:GridBoundColumn>
                    </Columns>
                </telerik:GridTableView>
            </DetailTables>
        </MasterTableView>
        <ClientSettings AllowKeyboardNavigation="true">
            <Selecting AllowRowSelect="true" />
            
        </ClientSettings>
    </telerik:RadGrid>
    <input type="button" type="button" value="Print grid!" onclick="PrintRadGrid()" />
    <asp:Panel ID="panel1" runat="server" Visible="false">
        <asp:TextBox ID="box1" runat="server" Visible="false"></asp:TextBox>
    </asp:Panel>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT TOP 1 [CustomerID], [CompanyName], [ContactName], [Country] FROM [Customers]">
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT  [OrderID], [CustomerID], [OrderDate], [ShipCountry] FROM [Orders] Where CustomerID=@CustomerID">
        <SelectParameters>
            <asp:Parameter Name="CustomerID" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:Button ID="button1" runat="server" Text="Set visibility!" />
    </form>
</body>
</html>
