گرفتن خروجی PDF از RadGrid در ASP.NET

Export to PDF from a RadGrid

مقداری داده جهت خروجی گرفتن از RadGrid در نظر می گیریم :

Table

طراحی Grid :
Design of the Grid

<divclass="row TopBar"><%--   
    <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server">--%>  
        <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">  
            <ClientSettingsEnablePostBackOnRowClick="true">  
                <SelectingAllowRowSelect="true"/>  
            </ClientSettings>  
            <ExportSettingsExportOnlyData="true"Excel-Format="Html"OpenInNewWindow="true">  
                <PdfPageHeight="210mm"PageWidth="297mm"DefaultFontFamily="Arial Unicode MS"PageTopMargin="45mm"  
BorderStyle="Thin"BorderColor="#666666">  
                </Pdf>  
            </ExportSettings>  
            <MasterTableViewDataKeyNames="OrderId">  
                <CommandItemSettingsShowExportToExcelButton="true"/>  
                <Columns>  
                    <telerik:GridTemplateColumnFilterControlAltText="Filter TemplateColumn column"UniqueName="TemplateColumn"Display="true"AllowFiltering="false"ShowFilterIcon="false">  
                        <ItemTemplate><%#Container.DataSetIndex+1 %>  
                        </ItemTemplate>  
                    </telerik:GridTemplateColumn>  
                    <telerik:GridTemplateColumnFilterControlAltText="Filter TemplateColumn column"UniqueName="TemplateColumn2"Display="false"AllowFiltering="false"ShowFilterIcon="false"HeaderStyle-Width="100">  
                        <ItemTemplate>  
                            <asp:LabelID="LblOrderId"runat="server"Text='<%#Eval("OrderId") %>'>  
                            </asp:Label>  
                        </ItemTemplate>  
                    </telerik:GridTemplateColumn>  
                    <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="OrderId"HeaderText="ORDER ID"DataField="OrderId"Display="false"AllowFiltering="false"ShowFilterIcon="false">  
                        <ColumnValidationSettings>  
                            <ModelErrorMessageText=""/>  
                        </ColumnValidationSettings>  
                    </telerik:GridBoundColumn>  
                    <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="OrderNo"HeaderText="ORDER NO"DataField="OrderNo">  
                        <ColumnValidationSettings>  
                            <ModelErrorMessageText=""/>  
                        </ColumnValidationSettings>  
                    </telerik:GridBoundColumn>  
                    <telerik:GridDateTimeColumnFilterControlAltText="Filter column column"UniqueName="OrderDate"HeaderText="ORDER DATE"DataField="OrderDate"AllowFiltering="true"ShowFilterIcon="true"HeaderStyle-Width="150px"DataFormatString="{0:dd/MMM/yyyy}">  
                        <ColumnValidationSettings>  
                            <ModelErrorMessageText=""/>  
                        </ColumnValidationSettings>  
                    </telerik:GridDateTimeColumn>  
                    <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="ApplicantName"HeaderText="APPLICANT NAME"DataField="ApplicantName"HeaderStyle-Width="150px">  
                        <ColumnValidationSettings>  
                            <ModelErrorMessageText=""/>  
                        </ColumnValidationSettings>  
                    </telerik:GridBoundColumn>  
                    <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="ContactNo"HeaderText="CONTACT NO"DataField="ContactNo"AllowFiltering="true"ShowFilterIcon="true"HeaderStyle-Width="150px">  
                        <ColumnValidationSettings>  
                            <ModelErrorMessageText=""/>  
                        </ColumnValidationSettings>  
                    </telerik:GridBoundColumn>  
                    <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="Email"HeaderText="EMAIL"DataField="Email"AllowFiltering="true"ShowFilterIcon="true">  
                        <ColumnValidationSettings>  
                            <ModelErrorMessageText=""/>  
                        </ColumnValidationSettings>  
                    </telerik:GridBoundColumn>  
                    <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="ShippingAddress"HeaderText="SHIPPING ADDRESS"DataField="ShippingAddress"AllowFiltering="false"ShowFilterIcon="false"Display="false">  
                        <ColumnValidationSettings>  
                            <ModelErrorMessageText=""/>  
                        </ColumnValidationSettings>  
                    </telerik:GridBoundColumn>  
                    <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="Pincode"HeaderText="PIN CODE"DataField="Pincode"AllowFiltering="false"ShowFilterIcon="false"Display="false">  
                        <ColumnValidationSettings>  
                            <ModelErrorMessageText=""/>  
                        </ColumnValidationSettings>  
                    </telerik:GridBoundColumn>  
                    <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="CityName"HeaderText="CityName"DataField="CityName"AllowFiltering="false"ShowFilterIcon="false"Display="false">  
                        <ColumnValidationSettings>  
                            <ModelErrorMessageText=""/>  
                        </ColumnValidationSettings>  
                    </telerik:GridBoundColumn>  
                    <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="StateName"HeaderText="StateName"DataField="StateName"AllowFiltering="false"ShowFilterIcon="false"Display="false">  
                        <ColumnValidationSettings>  
                            <ModelErrorMessageText=""/>  
                        </ColumnValidationSettings>  
                    </telerik:GridBoundColumn>  
                    <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="CountryName"HeaderText="CountryName"DataField="CountryName"AllowFiltering="false"ShowFilterIcon="false"Display="false">  
                        <ColumnValidationSettings>  
                            <ModelErrorMessageText=""/>  
                        </ColumnValidationSettings>  
                    </telerik:GridBoundColumn>  
                    <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="PaymentId"HeaderText="Payment Id"DataField="PaymentId"AllowFiltering="false"ShowFilterIcon="false"Display="false">  
                        <ColumnValidationSettings>  
                            <ModelErrorMessageText=""/>  
                        </ColumnValidationSettings>  
                    </telerik:GridBoundColumn>  
                    <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="Prices"HeaderText="TOTAL AMOUNT"DataField="Prices"AllowFiltering="false"ShowFilterIcon="false">  
                        <ColumnValidationSettings>  
                            <ModelErrorMessageText=""/>  
                        </ColumnValidationSettings>  
                    </telerik:GridBoundColumn>  
                    <telerik:GridBoundColumnFilterControlAltText="Filter column column"UniqueName="Status"HeaderText="STATUS"DataField="Status">  
                        <ColumnValidationSettings>  
                            <ModelErrorMessageText=""/>  
                        </ColumnValidationSettings>  
                    </telerik:GridBoundColumn>  
                    <telerik:GridTemplateColumnAllowFiltering="false"ShowFilterIcon="false">  
                        <ItemTemplate>  
                            <telerik:RadButtonID="RadBtnAction"runat="server"Text="Action"Skin="Glow"CommandName="cmd_action"CommandArgument='<%#Eval("OrderId") %>'>  
                            </telerik:RadButton>  
                        </ItemTemplate>  
                    </telerik:GridTemplateColumn>  
                </Columns>  
                <NestedViewTemplate>  
                    <telerik:RadGridID="RadGridChild"runat="server"AutoGenerateColumns="false"GroupPanelPosition="Top"Skin="Silk">  
                        <ClientSettings>  
                            <SelectingAllowRowSelect="true"/>  
                        </ClientSettings>  
                        <MasterTableViewDataKeyNames="OrderId">  
                            <Columns>  
                                <telerik:GridTemplateColumnFilterControlAltText="Filter TemplateColumn column"UniqueName="TemplateColumn1"Display="false"AllowFiltering="false"ShowFilterIcon="false">  
                                    <ItemTemplate><%#Container.DataSetIndex+1 %>  
                                    </ItemTemplate>  
                                </telerik:GridTemplateColumn>  
                                <telerik:GridTemplateColumnFilterControlAltText="Filter TemplateColumn column"UniqueName="TemplateColumn3"Display="true"AllowFiltering="false"ShowFilterIcon="false">  
                                    <ItemTemplate>  
                                        <tablestyle="width: 100%;">  
                                            <tr>  
                                                <tdstyle="width: 20px;">  
                                                </td>  
                                                <tdstyle="width: 150px;">  
                                                </td>  
                                                <tdstyle="width: 2px;">  
                                                </td>  
                                                <tdstyle="width: 200px;">  
                                                </td>  
                                                <td></td>  
                                            </tr>  
                                            <tr>  
                                                <td></td>  
                                                <td>Your Application No</td>  
                                                <td>:</td>  
                                                <td>  
                                                    <asp:LabelID="LblApplicationNo"runat="server"Text='<%#Eval("ApplicationNo") %>'>  
                                                    </asp:Label>  
                                                </td>  
                                                <td></td>  
                                            </tr>  
                                            <tr>  
                                                <td></td>  
                                                <td>Order No</td>  
                                                <td>:</td>  
                                                <td>  
                                                    <asp:LabelID="LblOrderNo"runat="server"Text='<%#Eval("OrderNo") %>'>  
                                                    </asp:Label>  
                                                </td>  
                                                <td></td>  
                                            </tr>  
                                            <tr>  
                                                <td></td>  
                                                <td>Payment Mode</td>  
                                                <td>:</td>  
                                                <td>  
                                                    <asp:LabelID="LblPaymentMode"runat="server"Text='<%#Eval("PaymentMode") %>'>  
                                                    </asp:Label>  
                                                </td>  
                                                <td></td>  
                                            </tr>  
                                            <tr>  
                                                <td></td>  
                                                <tdcolspan="4">  
                                                    <tablestyle="width:350px; border:1px solid black;">  
                                                        <tr>  
                                                            <tdstyle="width:144px;"class="InnerTblHeadTop">Item Name  
                                                            </td>  
                                                            <tdclass="InnerTblHeadTop">  
                                                            </td>  
                                                            <tdclass="InnerTblHeadTop">Amount  
                                                            </td>  
                                                        </tr>  
                                                        <tr>  
                                                            <td>Prospectus Fees</td>  
                                                            <td>:</td>  
                                                            <td>  
                                                                <asp:LabelID="LblProspectusFees"runat="server"Text='<%#Eval("ProspectusFees") %>'>  
                                                                </asp:Label>  
                                                            </td>  
                                                        </tr>  
                                                        <tr>  
                                                            <td>EMD1</td>  
                                                            <td>:</td>  
                                                            <td>  
                                                                <asp:LabelID="LblEMD1"runat="server"Text='<%#Eval("EMD1") %>'>  
                                                                </asp:Label>  
                                                            </td>  
                                                        </tr>  
                                                        <tr>  
                                                            <tdclass="InnerTblHead">Total Amount  
                                                            </td>  
                                                            <tdclass="InnerTblHead">  
                                                            </td>  
                                                            <tdclass="InnerTblHead">  
                                                                <asp:LabelID="LblTotalPrice"runat="server"Text='<%#Eval("Prices") %>'>  
                                                                </asp:Label>  
                                                            </td>  
                                                        </tr>  
                                                    </table>  
                                                </td>  
                                            </tr>  
                                            <tr>  
                                                <td></td>  
                                                <td>Email</td>  
                                                <td>:</td>  
                                                <td>  
                                                    <asp:LabelID="LblEmail"runat="server"Text='<%#Eval("Email") %>'>  
                                                    </asp:Label>  
                                                </td>  
                                                <td></td>  
                                            </tr>  
                                            <tr>  
                                                <td></td>  
                                                <td>Shipping Address</td>  
                                                <td>:</td>  
                                                <tdcolspan="2">  
                                                    <asp:LabelID="Label1"runat="server"Text='<%#Eval("ShippingAddress") %>'>  
                                                    </asp:Label>  
                                                </td>  
                                            </tr>  
                                            <tr>  
                                                <td></td>  
                                                <td>City Name</td>  
                                                <td>:</td>  
                                                <td>  
                                                    <asp:LabelID="Label3"runat="server"Text='<%#Eval("CityName") %>'>  
                                                    </asp:Label>  
                                                </td>  
                                                <tdstyle="width:150px;">State Name  
                                                </td>  
                                                <td>:</td>  
                                                <td>  
                                                    <asp:LabelID="Label4"runat="server"Text='<%#Eval("StateName") %>'>  
                                                    </asp:Label>  
                                                </td>  
                                                <tdstyle="width:150px;">Country Name  
                                                </td>  
                                                <td>:</td>  
                                                <td>  
                                                    <asp:LabelID="Label5"runat="server"Text='<%#Eval("CountryName") %>'>  
                                                    </asp:Label>  
                                                </td>  
                                            </tr>  
                                            <tr>  
                                                <td></td>  
                                                <td>Pincode</td>  
                                                <td>:</td>  
                                                <td>  
                                                    <asp:LabelID="Label2"runat="server"Text='<%#Eval("Pincode") %>'>  
                                                    </asp:Label>  
                                                </td>  
                                                <td></td>  
                                            </tr>  
                                            <tr>  
                                                <td></td>  
                                                <td></td>  
                                            </tr>  
                                        </table>  
                                        <tablestyle="width:100%;">  
                                            <tr>  
                                                <tdstyle="width:3px;">  
                                                </td>  
                                                <td>Thank you, for applying in the Kencloud Partnership Program. Your Application   
                                                    <b><%#Eval("ApplicationNo") %>  
                                                    </b> has been successfully submitted  
  
                                                    <br/>  
and Order No.  
                                                    <b><%#Eval("OrderNo") %>  
                                                    </b>has been confirmed for the value of INR   
                                                    <b><%#Eval("Prices") %>  
                                                    </b> . The application prospectus kit along with the  
  
                                                    <br/>  
reciept will be dispatched to your given address shortly.  
  
                                                </td>  
                                            </tr>  
                                        </table>  
                                    </ItemTemplate>  
                                </telerik:GridTemplateColumn>  
                            </Columns>  
                        </MasterTableView>  
                    </telerik:RadGrid>  
                </NestedViewTemplate>  
            </MasterTableView>  
        </telerik:RadGrid><%--   
    </telerik:RadAjaxPanel>--%>  
</div>

کدهای فایل .cs

فرمت بندی PDF

#region--Declaring Head Part  
for The Pdf Export--  
stringheaderMiddleCell = "<h2>My Contact Details </h2>";  
stringheaderLeftCell = "<imgsrc=\"Images/sushibar.jpg\" alt=\"sushi bar\" style=\"width:64px; height: 64px;\" />";  
stringfooterMiddleCell = "<?page-number?>";#  
endregion  
privatevoidExportToPdf()  
{#region--Export Selected Data To Pdf--  
    try  
    {  
        RadGridTotalCOD.AllowPaging = true;  
        RadGridTotalCOD.AllowFilteringByColumn = false;  
        // loading the Grid with data  
        LoadTotalCod();#  
        region--Export with format removed--  
        RadGridTotalCOD.ExportSettings.ExportOnlyData = true;  
        RadGridTotalCOD.ExportSettings.Pdf.BorderStyle = GridPdfSettings.GridPdfBorderStyle.Thin;  
        RadGridTotalCOD.ExportSettings.Pdf.BorderType = GridPdfSettings.GridPdfBorderType.NoBorder;  
        RadGridTotalCOD.ExportSettings.Pdf.BorderType = GridPdfSettings.GridPdfBorderType.AllBorders;  
        RadGridTotalCOD.ExportSettings.Pdf.PageHeader.MiddleCell.Text = headerMiddleCell;  
        RadGridTotalCOD.ExportSettings.Pdf.PageHeader.MiddleCell.TextAlign = GridPdfPageHeaderFooterCell.CellTextAlign.Center;  
        // RadGridTotalCOD.ExportSettings.Pdf.PageHeader.LeftCell.Text = headerLeftCell;  
        RadGridTotalCOD.ExportSettings.Pdf.PageHeader.LeftCell.TextAlign = GridPdfPageHeaderFooterCell.CellTextAlign.Center;  
        RadGridTotalCOD.ExportSettings.Pdf.PageFooter.MiddleCell.Text = footerMiddleCell;  
        RadGridTotalCOD.ExportSettings.Pdf.PageFooter.MiddleCell.TextAlign = GridPdfPageHeaderFooterCell.CellTextAlign.Center;#  
        endregion  
        string Date = DateTime.Now.ToString("dd/MMM/yyyy");  
        RadGridTotalCOD.ExportSettings.FileName = "Client Address " + " " + Date;#  
        region--Making Visible false some of the column before exporting--  
        // TemplateColumn, TemplateColumn2, OrderId, OrderNo, OrderDate, Email, Prices, Status  
        (RadGridTotalCOD.MasterTableView.GetColumn("TemplateColumn") asGridTemplateColumn).Visible = false;  
        (RadGridTotalCOD.MasterTableView.GetColumn("TemplateColumn") asGridTemplateColumn).Display = false;  
        (RadGridTotalCOD.MasterTableView.GetColumn("TemplateColumn2") asGridTemplateColumn).Visible = false;  
        (RadGridTotalCOD.MasterTableView.GetColumn("TemplateColumn2") asGridTemplateColumn).Display = false;  
        (RadGridTotalCOD.MasterTableView.GetColumn("OrderId") asGridBoundColumn).Visible = false;  
        (RadGridTotalCOD.MasterTableView.GetColumn("OrderId") asGridBoundColumn).Display = false;  
        (RadGridTotalCOD.MasterTableView.GetColumn("OrderNo") asGridBoundColumn).Visible = false;  
        (RadGridTotalCOD.MasterTableView.GetColumn("OrderNo") asGridBoundColumn).Display = false;  
        (RadGridTotalCOD.MasterTableView.GetColumn("OrderDate") asGridBoundColumn).Visible = false;  
        (RadGridTotalCOD.MasterTableView.GetColumn("OrderDate") asGridBoundColumn).Display = false;  
        (RadGridTotalCOD.MasterTableView.GetColumn("Email") asGridBoundColumn).Visible = false;  
        (RadGridTotalCOD.MasterTableView.GetColumn("Email") asGridBoundColumn).Display = false;  
        (RadGridTotalCOD.MasterTableView.GetColumn("Prices") asGridBoundColumn).Visible = false;  
        (RadGridTotalCOD.MasterTableView.GetColumn("Prices") asGridBoundColumn).Display = false;  
        (RadGridTotalCOD.MasterTableView.GetColumn("Status") asGridBoundColumn).Visible = false;  
        (RadGridTotalCOD.MasterTableView.GetColumn("Status") asGridBoundColumn).Display = false;#  
        endregion# region--Making visible true on a Regular Basis--(RadGridTotalCOD.MasterTableView.GetColumn("ShippingAddress") asGridBoundColumn).Visible = true;  
        (RadGridTotalCOD.MasterTableView.GetColumn("ShippingAddress") asGridBoundColumn).Display = true;  
        (RadGridTotalCOD.MasterTableView.GetColumn("Pincode") asGridBoundColumn).Visible = true;  
        (RadGridTotalCOD.MasterTableView.GetColumn("Pincode") asGridBoundColumn).Display = true;  
        (RadGridTotalCOD.MasterTableView.GetColumn("CityName") asGridBoundColumn).Visible = true;  
        (RadGridTotalCOD.MasterTableView.GetColumn("CityName") asGridBoundColumn).Display = true;  
        (RadGridTotalCOD.MasterTableView.GetColumn("StateName") asGridBoundColumn).Visible = true;  
        (RadGridTotalCOD.MasterTableView.GetColumn("StateName") asGridBoundColumn).Display = true;  
        (RadGridTotalCOD.MasterTableView.GetColumn("CountryName") asGridBoundColumn).Visible = true;  
        (RadGridTotalCOD.MasterTableView.GetColumn("CountryName") asGridBoundColumn).Display = true;#  
        endregion  
        // Export Only the Parent Grid Data not tHe Inner Grid  
        RadGridTotalCOD.MasterTableView.HierarchyDefaultExpanded = false; // first level  
        RadGridTotalCOD.MasterTableView.ExportToPdf();  
        // Making visible true   
        RadGridTotalCOD.AllowFilteringByColumn = true;  
    }  
    catch (ArgumentNullExceptionExc)  
    {  
        LblErrorMsg.Text = "Application Error : " + Exc.Message;  
    }  
    catch (InvalidCastExceptionExc)  
    {  
        LblErrorMsg.Text = "Application Error : " + Exc.Message;  
    }  
    catch (InvalidOperationExceptionExc)  
    {  
        LblErrorMsg.Text = "Application Error : " + Exc.Message;  
    }  
    catch (FormatExceptionExc)  
    {  
        LblErrorMsg.Text = "Application Error : " + Exc.Message;  
    }  
    catch (ExceptionExc)  
    {  
        LblErrorMsg.Text = "Application Error : " + Exc.Message;  
    }#  
    endregion  
}

 

ورکشاپ رایگان دوره های تخصصی برنامه نویسی

شما این فرصت را دارید، با تکمیل فرم زیر، قبل از انتخاب دوره آموزشی مناسب خود، در ورکشاپ رایگان دوره های تخصصی برنامه نویسی شرکت کنید
  • این فیلد برای اعتبار سنجی است و باید بدون تغییر باقی بماند .

درباره‌ی محمد آذرنیوا

من محمد آذرنیوا، نویسنده و مدرس دوره های برنامه نویسی ، طراحی وب و تحلیل گر پایگاه داده هستم و قصد دارم در این وبسایت مطالب کاربردی در این زمینه را با شما به اشتراک بگذارم ...

همچنین ببینید

دوره آموزش طراحی سایت با ASP.NET مشهد

آموزش طراحی سایت با ASP.NET مشهد

آموزش طراحی سایت مشهد (دوره آموزشی) : تکنولوژی ASP.NET ، یک تکنولوژی روز برای ساخت صفحات …

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *