Back to Devexpress

Export Grid View Data

aspnet-3791-components-grid-view-concepts-export.md

latest7.0 KB
Original Source

Export Grid View Data

  • Jun 30, 2022
  • 3 minutes to read

The ASPxGridView allows you to export data to a file or stream in PDF, RTF, CSV, XLS, XLSX, and DOCX formats. To allow users to export data in your application, you can either rely on the grid’s built-in UI or use the client and server APIs in code.

Important

DevExpress controls require the DevExpress.RichEdit.v25.2.Export.dll library to export their content in DOCX or RTF format.

The export commands and the client API are disabled for security reasons (the default setting). To enable this functionality, set the EnableClientSideExportAPI property to true.

Toolbar Commands

The ASPxGridView toolbar implements the following commands to export grid data: ExportToCsv, ExportToPdf, ExportToRtf, ExportToDocx, ExportToXls, and ExportToXlsx.

Populate a grid toolbar with a GridViewToolbarItem object and set its Command property to an export command to add the corresponding item to the toolbar.

Run Demo: Grid - ToolbarRun Demo: Grid - Exporting to Different Formats

aspx
<dx:ASPxGridView ID="Grid" runat="server" KeyFieldName="ProductID" DataSourceID="ProductsDataSource">
    <SettingsExport EnableClientSideExportAPI="true"/>
    <Toolbars>
        <dx:GridViewToolbar>
            <Items>
                <dx:GridViewToolbarItem Command="ExportToPdf" />
                <dx:GridViewToolbarItem Command="ExportToDocx" />
                <dx:GridViewToolbarItem Command="ExportToXlsx"/>
                <!-- ... -->

Context Menu Items

The ASPxGridView context menu implements the Export submenu that contains the following commands: ExportToCsv, ExportToPdf, ExportToRtf, ExportToDocx, ExportToXls, and ExportToXlsx.

Run Demo: Grid - Context Menu

To enable the Export menu, set the Visible property to true.

aspx
<dx:ASPxGridView ID="Grid" runat="server" KeyFieldName="ProductID" DataSourceID="ProductsDataSource">
    <SettingsExport EnableClientSideExportAPI="true"/>
    <SettingsContextMenu Enabled="true">
        <RowMenuItemVisibility ExportMenu-Visible="true" />
    </SettingsContextMenu>
    ...

You can control the visibility of a particular export command in the Export submenu. Set a command’s visibility property to false to hide the corresponding item in the menu.

ItemVisibility Property
Export to PDFExportToPdf
Export to RTFExportToRtf
Export to DOCXExportToDocx
Export to CSVExportToCsv
Export to XLSExportToXls
Export to XLSXExportToXlsx

aspx
<dx:ASPxGridView ID="Grid" runat="server" KeyFieldName="ProductID" DataSourceID="ProductsDataSource">
    <SettingsExport EnableClientSideExportAPI="true"/>
    <SettingsContextMenu Enabled="true">
        <RowMenuItemVisibility>
            <ExportMenu Visible="true" ExportToCsv="false" ExportToRtf="false" ExportToDocx="false"/>
        </RowMenuItemVisibility>
    </SettingsContextMenu>
    ...

Server API

The ASPxGridView component implements the following export methods:

FormatExport to a StreamExport to the Response
CSVExportToCsvExportCsvToResponse
DOCXExportToDocxExportDocxToResponse
PDFExportToPdfExportPdfToResponse
RTFExportToRtfExportRtfToResponse
XLSExportToXlsExportXlsToResponse
XLSXExportToXlsxExportXlsxToResponse
aspx
<dx:ASPxGridView ID="ASPxGridView1" runat="server" DataSourceID="CustomerReportsDataSource" />
<dx:ASPxButton ID="ASPxButton1" runat="server" Text="Export to XLS" OnClick="ASPxButton1_Click" />
csharp
protected void ASPxButton1_Click(object sender, EventArgs e) {
    ASPxGridView1.ExportPdfToResponse();
}
vb
Protected Sub ASPxButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
    ASPxGridView1.ExportPdfToResponse()
End Sub

Client API

Call the client ExportTo(format) method to export grid data to a file in the specified format.

aspx
<dx:ASPxGridView runat="server" ID="ASPxGridView1" ClientInstanceName="grid" 
    DataSourceID="CustomerReportsDataSource">
    <SettingsExport EnableClientSideExportAPI="true"/>
</dx:ASPxGridView>

<dx:ASPxButton ID="ASPxButton1" runat="server" AutoPostBack="False" Text="Export to XLS">
    <ClientSideEvents Click="function(s, e) {
        grid.ExportTo(ASPxClientGridViewExportFormat.Xls);
    }" />
</dx:ASPxButton>