Export to PDF from a RadGrid in ASP.NET

Export to PDF from a RadGrid

I am using the data from the RadGrid here to export.

Table

Design of the Grid

  1. <divclass="row TopBar"><%--   
  2.     <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server">--%>  
  3.         <telerik:RadGridID="RadGridTotalCOD"runat="server"AutoGenerateColumns="False"GroupPanelPosition="Top"AllowPaging="True"AllowFilteringByColumn="True"AllowMultiRowSelection="True"Skin="Outlook"OnItemCommand="RadGridTotalCOD_ItemCommand"OnPageIndexChanged="RadGridTotalCOD_PageIndexChanged"OnPageSizeChanged="RadGridTotalCOD_PageSizeChanged"PageSize="8"OnPdfExporting="RadGridTotalCOD_PdfExporting">  
  4.             <ClientSettingsEnablePostBackOnRowClick="true">  
  5.                 <SelectingAllowRowSelect="true"/>  
  6.             </ClientSettings>  
  7.             <ExportSettingsExportOnlyData="true"Excel-Format="Html"OpenInNewWindow="true">  
  8.                 <PdfPageHeight="210mm"PageWidth="297mm"DefaultFontFamily="Arial Unicode MS"PageTopMargin="45mm"  
  9. BorderStyle="Thin"BorderColor="#666666">  
  10.                 </Pdf>  
  11.             </ExportSettings>  
  12.             <MasterTableViewDataKeyNames="OrderId">  
  13.                 <CommandItemSettingsShowExportToExcelButton="true"/>  
  14.                 <Columns>  
  15.                     <telerik:GridTemplateColumnFilterControlAltText="Filter TemplateColumn column"UniqueName="TemplateColumn"Display="true"AllowFiltering="false"ShowFilterIcon="false">  
  16.                         <ItemTemplate><%#Container.DataSetIndex+1 %>  
  17.                         </ItemTemplate>  
  18.                     </telerik:GridTemplateColumn>  
  19.                     <telerik:GridTemplateColumnFilterControlAltText="Filter TemplateColumn column"UniqueName="TemplateColumn2"Display="false"AllowFiltering="false"ShowFilterIcon="false"HeaderStyle-Width="100">  
  20.                         <ItemTemplate>  
  21.                             <asp:LabelID="LblOrderId"runat="server"Text='<%#Eval("OrderId") %>'>  
  22.                             </asp:Label>  
  23.                         </ItemTemplate>  
  24.                     </telerik:GridTemplateColumn>  
  25.                     <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="OrderId"HeaderText="ORDER ID"DataField="OrderId"Display="false"AllowFiltering="false"ShowFilterIcon="false">  
  26.                         <ColumnValidationSettings>  
  27.                             <ModelErrorMessageText=""/>  
  28.                         </ColumnValidationSettings>  
  29.                     </telerik:GridBoundColumn>  
  30.                     <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="OrderNo"HeaderText="ORDER NO"DataField="OrderNo">  
  31.                         <ColumnValidationSettings>  
  32.                             <ModelErrorMessageText=""/>  
  33.                         </ColumnValidationSettings>  
  34.                     </telerik:GridBoundColumn>  
  35.                     <telerik:GridDateTimeColumnFilterControlAltText="Filter column column"UniqueName="OrderDate"HeaderText="ORDER DATE"DataField="OrderDate"AllowFiltering="true"ShowFilterIcon="true"HeaderStyle-Width="150px"DataFormatString="{0:dd/MMM/yyyy}">  
  36.                         <ColumnValidationSettings>  
  37.                             <ModelErrorMessageText=""/>  
  38.                         </ColumnValidationSettings>  
  39.                     </telerik:GridDateTimeColumn>  
  40.                     <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="ApplicantName"HeaderText="APPLICANT NAME"DataField="ApplicantName"HeaderStyle-Width="150px">  
  41.                         <ColumnValidationSettings>  
  42.                             <ModelErrorMessageText=""/>  
  43.                         </ColumnValidationSettings>  
  44.                     </telerik:GridBoundColumn>  
  45.                     <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="ContactNo"HeaderText="CONTACT NO"DataField="ContactNo"AllowFiltering="true"ShowFilterIcon="true"HeaderStyle-Width="150px">  
  46.                         <ColumnValidationSettings>  
  47.                             <ModelErrorMessageText=""/>  
  48.                         </ColumnValidationSettings>  
  49.                     </telerik:GridBoundColumn>  
  50.                     <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="Email"HeaderText="EMAIL"DataField="Email"AllowFiltering="true"ShowFilterIcon="true">  
  51.                         <ColumnValidationSettings>  
  52.                             <ModelErrorMessageText=""/>  
  53.                         </ColumnValidationSettings>  
  54.                     </telerik:GridBoundColumn>  
  55.                     <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="ShippingAddress"HeaderText="SHIPPING ADDRESS"DataField="ShippingAddress"AllowFiltering="false"ShowFilterIcon="false"Display="false">  
  56.                         <ColumnValidationSettings>  
  57.                             <ModelErrorMessageText=""/>  
  58.                         </ColumnValidationSettings>  
  59.                     </telerik:GridBoundColumn>  
  60.                     <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="Pincode"HeaderText="PIN CODE"DataField="Pincode"AllowFiltering="false"ShowFilterIcon="false"Display="false">  
  61.                         <ColumnValidationSettings>  
  62.                             <ModelErrorMessageText=""/>  
  63.                         </ColumnValidationSettings>  
  64.                     </telerik:GridBoundColumn>  
  65.                     <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="CityName"HeaderText="CityName"DataField="CityName"AllowFiltering="false"ShowFilterIcon="false"Display="false">  
  66.                         <ColumnValidationSettings>  
  67.                             <ModelErrorMessageText=""/>  
  68.                         </ColumnValidationSettings>  
  69.                     </telerik:GridBoundColumn>  
  70.                     <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="StateName"HeaderText="StateName"DataField="StateName"AllowFiltering="false"ShowFilterIcon="false"Display="false">  
  71.                         <ColumnValidationSettings>  
  72.                             <ModelErrorMessageText=""/>  
  73.                         </ColumnValidationSettings>  
  74.                     </telerik:GridBoundColumn>  
  75.                     <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="CountryName"HeaderText="CountryName"DataField="CountryName"AllowFiltering="false"ShowFilterIcon="false"Display="false">  
  76.                         <ColumnValidationSettings>  
  77.                             <ModelErrorMessageText=""/>  
  78.                         </ColumnValidationSettings>  
  79.                     </telerik:GridBoundColumn>  
  80.                     <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="PaymentId"HeaderText="Payment Id"DataField="PaymentId"AllowFiltering="false"ShowFilterIcon="false"Display="false">  
  81.                         <ColumnValidationSettings>  
  82.                             <ModelErrorMessageText=""/>  
  83.                         </ColumnValidationSettings>  
  84.                     </telerik:GridBoundColumn>  
  85.                     <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="Prices"HeaderText="TOTAL AMOUNT"DataField="Prices"AllowFiltering="false"ShowFilterIcon="false">  
  86.                         <ColumnValidationSettings>  
  87.                             <ModelErrorMessageText=""/>  
  88.                         </ColumnValidationSettings>  
  89.                     </telerik:GridBoundColumn>  
  90.                     <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="Status"HeaderText="STATUS"DataField="Status">  
  91.                         <ColumnValidationSettings>  
  92.                             <ModelErrorMessageText=""/>  
  93.                         </ColumnValidationSettings>  
  94.                     </telerik:GridBoundColumn>  
  95.                     <telerik:GridTemplateColumnAllowFiltering="false"ShowFilterIcon="false">  
  96.                         <ItemTemplate>  
  97.                             <telerik:RadButtonID="RadBtnAction"runat="server"Text="Action"Skin="Glow"CommandName="cmd_action"CommandArgument='<%#Eval("OrderId") %>'>  
  98.                             </telerik:RadButton>  
  99.                         </ItemTemplate>  
  100.                     </telerik:GridTemplateColumn>  
  101.                 </Columns>  
  102.                 <NestedViewTemplate>  
  103.                     <telerik:RadGridID="RadGridChild"runat="server"AutoGenerateColumns="false"GroupPanelPosition="Top"Skin="Silk">  
  104.                         <ClientSettings>  
  105.                             <SelectingAllowRowSelect="true"/>  
  106.                         </ClientSettings>  
  107.                         <MasterTableViewDataKeyNames="OrderId">  
  108.                             <Columns>  
  109.                                 <telerik:GridTemplateColumnFilterControlAltText="Filter TemplateColumn column"UniqueName="TemplateColumn1"Display="false"AllowFiltering="false"ShowFilterIcon="false">  
  110.                                     <ItemTemplate><%#Container.DataSetIndex+1 %>  
  111.                                     </ItemTemplate>  
  112.                                 </telerik:GridTemplateColumn>  
  113.                                 <telerik:GridTemplateColumnFilterControlAltText="Filter TemplateColumn column"UniqueName="TemplateColumn3"Display="true"AllowFiltering="false"ShowFilterIcon="false">  
  114.                                     <ItemTemplate>  
  115.                                         <tablestyle="width: 100%;">  
  116.                                             <tr>  
  117.                                                 <tdstyle="width: 20px;">  
  118.                                                 </td>  
  119.                                                 <tdstyle="width: 150px;">  
  120.                                                 </td>  
  121.                                                 <tdstyle="width: 2px;">  
  122.                                                 </td>  
  123.                                                 <tdstyle="width: 200px;">  
  124.                                                 </td>  
  125.                                                 <td></td>  
  126.                                             </tr>  
  127.                                             <tr>  
  128.                                                 <td></td>  
  129.                                                 <td>Your Application No</td>  
  130.                                                 <td>:</td>  
  131.                                                 <td>  
  132.                                                     <asp:LabelID="LblApplicationNo"runat="server"Text='<%#Eval("ApplicationNo") %>'>  
  133.                                                     </asp:Label>  
  134.                                                 </td>  
  135.                                                 <td></td>  
  136.                                             </tr>  
  137.                                             <tr>  
  138.                                                 <td></td>  
  139.                                                 <td>Order No</td>  
  140.                                                 <td>:</td>  
  141.                                                 <td>  
  142.                                                     <asp:LabelID="LblOrderNo"runat="server"Text='<%#Eval("OrderNo") %>'>  
  143.                                                     </asp:Label>  
  144.                                                 </td>  
  145.                                                 <td></td>  
  146.                                             </tr>  
  147.                                             <tr>  
  148.                                                 <td></td>  
  149.                                                 <td>Payment Mode</td>  
  150.                                                 <td>:</td>  
  151.                                                 <td>  
  152.                                                     <asp:LabelID="LblPaymentMode"runat="server"Text='<%#Eval("PaymentMode") %>'>  
  153.                                                     </asp:Label>  
  154.                                                 </td>  
  155.                                                 <td></td>  
  156.                                             </tr>  
  157.                                             <tr>  
  158.                                                 <td></td>  
  159.                                                 <tdcolspan="4">  
  160.                                                     <tablestyle="width:350px; border:1px solid black;">  
  161.                                                         <tr>  
  162.                                                             <tdstyle="width:144px;"class="InnerTblHeadTop">Item Name  
  163.                                                             </td>  
  164.                                                             <tdclass="InnerTblHeadTop">  
  165.                                                             </td>  
  166.                                                             <tdclass="InnerTblHeadTop">Amount  
  167.                                                             </td>  
  168.                                                         </tr>  
  169.                                                         <tr>  
  170.                                                             <td>Prospectus Fees</td>  
  171.                                                             <td>:</td>  
  172.                                                             <td>  
  173.                                                                 <asp:LabelID="LblProspectusFees"runat="server"Text='<%#Eval("ProspectusFees") %>'>  
  174.                                                                 </asp:Label>  
  175.                                                             </td>  
  176.                                                         </tr>  
  177.                                                         <tr>  
  178.                                                             <td>EMD1</td>  
  179.                                                             <td>:</td>  
  180.                                                             <td>  
  181.                                                                 <asp:LabelID="LblEMD1"runat="server"Text='<%#Eval("EMD1") %>'>  
  182.                                                                 </asp:Label>  
  183.                                                             </td>  
  184.                                                         </tr>  
  185.                                                         <tr>  
  186.                                                             <tdclass="InnerTblHead">Total Amount  
  187.                                                             </td>  
  188.                                                             <tdclass="InnerTblHead">  
  189.                                                             </td>  
  190.                                                             <tdclass="InnerTblHead">  
  191.                                                                 <asp:LabelID="LblTotalPrice"runat="server"Text='<%#Eval("Prices") %>'>  
  192.                                                                 </asp:Label>  
  193.                                                             </td>  
  194.                                                         </tr>  
  195.                                                     </table>  
  196.                                                 </td>  
  197.                                             </tr>  
  198.                                             <tr>  
  199.                                                 <td></td>  
  200.                                                 <td>Email</td>  
  201.                                                 <td>:</td>  
  202.                                                 <td>  
  203.                                                     <asp:LabelID="LblEmail"runat="server"Text='<%#Eval("Email") %>'>  
  204.                                                     </asp:Label>  
  205.                                                 </td>  
  206.                                                 <td></td>  
  207.                                             </tr>  
  208.                                             <tr>  
  209.                                                 <td></td>  
  210.                                                 <td>Shipping Address</td>  
  211.                                                 <td>:</td>  
  212.                                                 <tdcolspan="2">  
  213.                                                     <asp:LabelID="Label1"runat="server"Text='<%#Eval("ShippingAddress") %>'>  
  214.                                                     </asp:Label>  
  215.                                                 </td>  
  216.                                             </tr>  
  217.                                             <tr>  
  218.                                                 <td></td>  
  219.                                                 <td>City Name</td>  
  220.                                                 <td>:</td>  
  221.                                                 <td>  
  222.                                                     <asp:LabelID="Label3"runat="server"Text='<%#Eval("CityName") %>'>  
  223.                                                     </asp:Label>  
  224.                                                 </td>  
  225.                                                 <tdstyle="width:150px;">State Name  
  226.                                                 </td>  
  227.                                                 <td>:</td>  
  228.                                                 <td>  
  229.                                                     <asp:LabelID="Label4"runat="server"Text='<%#Eval("StateName") %>'>  
  230.                                                     </asp:Label>  
  231.                                                 </td>  
  232.                                                 <tdstyle="width:150px;">Country Name  
  233.                                                 </td>  
  234.                                                 <td>:</td>  
  235.                                                 <td>  
  236.                                                     <asp:LabelID="Label5"runat="server"Text='<%#Eval("CountryName") %>'>  
  237.                                                     </asp:Label>  
  238.                                                 </td>  
  239.                                             </tr>  
  240.                                             <tr>  
  241.                                                 <td></td>  
  242.                                                 <td>Pincode</td>  
  243.                                                 <td>:</td>  
  244.                                                 <td>  
  245.                                                     <asp:LabelID="Label2"runat="server"Text='<%#Eval("Pincode") %>'>  
  246.                                                     </asp:Label>  
  247.                                                 </td>  
  248.                                                 <td></td>  
  249.                                             </tr>  
  250.                                             <tr>  
  251.                                                 <td></td>  
  252.                                                 <td></td>  
  253.                                             </tr>  
  254.                                         </table>  
  255.                                         <tablestyle="width:100%;">  
  256.                                             <tr>  
  257.                                                 <tdstyle="width:3px;">  
  258.                                                 </td>  
  259.                                                 <td>Thank you, for applying in the Kencloud Partnership Program. Your Application   
  260.                                                     <b><%#Eval("ApplicationNo") %>  
  261.                                                     </b> has been successfully submitted  
  262.   
  263.                                                     <br/>  
  264. and Order No.  
  265.                                                     <b><%#Eval("OrderNo") %>  
  266.                                                     </b>has been confirmed for the value of INR   
  267.                                                     <b><%#Eval("Prices") %>  
  268.                                                     </b> . The application prospectus kit along with the  
  269.   
  270.                                                     <br/>  
  271. reciept will be dispatched to your given address shortly.  
  272.   
  273.                                                 </td>  
  274.                                             </tr>  
  275.                                         </table>  
  276.                                     </ItemTemplate>  
  277.                                 </telerik:GridTemplateColumn>  
  278.                             </Columns>  
  279.                         </MasterTableView>  
  280.                     </telerik:RadGrid>  
  281.                 </NestedViewTemplate>  
  282.             </MasterTableView>  
  283.         </telerik:RadGrid><%--   
  284.     </telerik:RadAjaxPanel>--%>  
  285. </div>  
Code at the .cs file

Formatting The PDF
  1. #region--Declaring Head Part  
  2. for The Pdf Export--  
  3. stringheaderMiddleCell = "<h2>My Contact Details </h2>";  
  4. stringheaderLeftCell = "<imgsrc=\"Images/sushibar.jpg\" alt=\"sushi bar\" style=\"width:64px; height: 64px;\" />";  
  5. stringfooterMiddleCell = "<?page-number?>";#  
  6. endregion  
  7. privatevoidExportToPdf()  
  8. {#region--Export Selected Data To Pdf--  
  9.     try  
  10.     {  
  11.         RadGridTotalCOD.AllowPaging = true;  
  12.         RadGridTotalCOD.AllowFilteringByColumn = false;  
  13.         // loading the Grid with data  
  14.         LoadTotalCod();#  
  15.         region--Export with format removed--  
  16.         RadGridTotalCOD.ExportSettings.ExportOnlyData = true;  
  17.         RadGridTotalCOD.ExportSettings.Pdf.BorderStyle = GridPdfSettings.GridPdfBorderStyle.Thin;  
  18.         RadGridTotalCOD.ExportSettings.Pdf.BorderType = GridPdfSettings.GridPdfBorderType.NoBorder;  
  19.         RadGridTotalCOD.ExportSettings.Pdf.BorderType = GridPdfSettings.GridPdfBorderType.AllBorders;  
  20.         RadGridTotalCOD.ExportSettings.Pdf.PageHeader.MiddleCell.Text = headerMiddleCell;  
  21.         RadGridTotalCOD.ExportSettings.Pdf.PageHeader.MiddleCell.TextAlign = GridPdfPageHeaderFooterCell.CellTextAlign.Center;  
  22.         // RadGridTotalCOD.ExportSettings.Pdf.PageHeader.LeftCell.Text = headerLeftCell;  
  23.         RadGridTotalCOD.ExportSettings.Pdf.PageHeader.LeftCell.TextAlign = GridPdfPageHeaderFooterCell.CellTextAlign.Center;  
  24.         RadGridTotalCOD.ExportSettings.Pdf.PageFooter.MiddleCell.Text = footerMiddleCell;  
  25.         RadGridTotalCOD.ExportSettings.Pdf.PageFooter.MiddleCell.TextAlign = GridPdfPageHeaderFooterCell.CellTextAlign.Center;#  
  26.         endregion  
  27.         string Date = DateTime.Now.ToString("dd/MMM/yyyy");  
  28.         RadGridTotalCOD.ExportSettings.FileName = "Client Address " + " " + Date;#  
  29.         region--Making Visible false some of the column before exporting--  
  30.         // TemplateColumn, TemplateColumn2, OrderId, OrderNo, OrderDate, Email, Prices, Status  
  31.         (RadGridTotalCOD.MasterTableView.GetColumn("TemplateColumn") asGridTemplateColumn).Visible = false;  
  32.         (RadGridTotalCOD.MasterTableView.GetColumn("TemplateColumn") asGridTemplateColumn).Display = false;  
  33.         (RadGridTotalCOD.MasterTableView.GetColumn("TemplateColumn2") asGridTemplateColumn).Visible = false;  
  34.         (RadGridTotalCOD.MasterTableView.GetColumn("TemplateColumn2") asGridTemplateColumn).Display = false;  
  35.         (RadGridTotalCOD.MasterTableView.GetColumn("OrderId") asGridBoundColumn).Visible = false;  
  36.         (RadGridTotalCOD.MasterTableView.GetColumn("OrderId") asGridBoundColumn).Display = false;  
  37.         (RadGridTotalCOD.MasterTableView.GetColumn("OrderNo") asGridBoundColumn).Visible = false;  
  38.         (RadGridTotalCOD.MasterTableView.GetColumn("OrderNo") asGridBoundColumn).Display = false;  
  39.         (RadGridTotalCOD.MasterTableView.GetColumn("OrderDate") asGridBoundColumn).Visible = false;  
  40.         (RadGridTotalCOD.MasterTableView.GetColumn("OrderDate") asGridBoundColumn).Display = false;  
  41.         (RadGridTotalCOD.MasterTableView.GetColumn("Email") asGridBoundColumn).Visible = false;  
  42.         (RadGridTotalCOD.MasterTableView.GetColumn("Email") asGridBoundColumn).Display = false;  
  43.         (RadGridTotalCOD.MasterTableView.GetColumn("Prices") asGridBoundColumn).Visible = false;  
  44.         (RadGridTotalCOD.MasterTableView.GetColumn("Prices") asGridBoundColumn).Display = false;  
  45.         (RadGridTotalCOD.MasterTableView.GetColumn("Status") asGridBoundColumn).Visible = false;  
  46.         (RadGridTotalCOD.MasterTableView.GetColumn("Status") asGridBoundColumn).Display = false;#  
  47.         endregion# region--Making visible true on a Regular Basis--(RadGridTotalCOD.MasterTableView.GetColumn("ShippingAddress") asGridBoundColumn).Visible = true;  
  48.         (RadGridTotalCOD.MasterTableView.GetColumn("ShippingAddress") asGridBoundColumn).Display = true;  
  49.         (RadGridTotalCOD.MasterTableView.GetColumn("Pincode") asGridBoundColumn).Visible = true;  
  50.         (RadGridTotalCOD.MasterTableView.GetColumn("Pincode") asGridBoundColumn).Display = true;  
  51.         (RadGridTotalCOD.MasterTableView.GetColumn("CityName") asGridBoundColumn).Visible = true;  
  52.         (RadGridTotalCOD.MasterTableView.GetColumn("CityName") asGridBoundColumn).Display = true;  
  53.         (RadGridTotalCOD.MasterTableView.GetColumn("StateName") asGridBoundColumn).Visible = true;  
  54.         (RadGridTotalCOD.MasterTableView.GetColumn("StateName") asGridBoundColumn).Display = true;  
  55.         (RadGridTotalCOD.MasterTableView.GetColumn("CountryName") asGridBoundColumn).Visible = true;  
  56.         (RadGridTotalCOD.MasterTableView.GetColumn("CountryName") asGridBoundColumn).Display = true;#  
  57.         endregion  
  58.         // Export Only the Parent Grid Data not tHe Inner Grid  
  59.         RadGridTotalCOD.MasterTableView.HierarchyDefaultExpanded = false// first level  
  60.         RadGridTotalCOD.MasterTableView.ExportToPdf();  
  61.         // Making visible true   
  62.         RadGridTotalCOD.AllowFilteringByColumn = true;  
  63.     }  
  64.     catch (ArgumentNullExceptionExc)  
  65.     {  
  66.         LblErrorMsg.Text = "Application Error : " + Exc.Message;  
  67.     }  
  68.     catch (InvalidCastExceptionExc)  
  69.     {  
  70.         LblErrorMsg.Text = "Application Error : " + Exc.Message;  
  71.     }  
  72.     catch (InvalidOperationExceptionExc)  
  73.     {  
  74.         LblErrorMsg.Text = "Application Error : " + Exc.Message;  
  75.     }  
  76.     catch (FormatExceptionExc)  
  77.     {  
  78.         LblErrorMsg.Text = "Application Error : " + Exc.Message;  
  79.     }  
  80.     catch (ExceptionExc)  
  81.     {  
  82.         LblErrorMsg.Text = "Application Error : " + Exc.Message;  
  83.     }#  
  84.     endregion  
  85. }