Export Grid View Data
- 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.v24.1.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.
<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
.
To enable the Export menu, set the Visible property to true
.
<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.
Item | Visibility Property |
---|---|
Export to PDF | ExportToPdf |
Export to RTF | ExportToRtf |
Export to DOCX | ExportToDocx |
Export to CSV | ExportToCsv |
Export to XLS | ExportToXls |
Export to XLSX | ExportToXlsx |
<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:
Format | Export to a Stream | Export to the Response |
---|---|---|
CSV | ExportToCsv | ExportCsvToResponse |
DOCX | ExportToDocx | ExportDocxToResponse |
ExportToPdf | ExportPdfToResponse | |
RTF | ExportToRtf | ExportRtfToResponse |
XLS | ExportToXls | ExportXlsToResponse |
XLSX | ExportToXlsx | ExportXlsxToResponse |
<dx:ASPxGridView ID="ASPxGridView1" runat="server" DataSourceID="CustomerReportsDataSource" />
<dx:ASPxButton ID="ASPxButton1" runat="server" Text="Export to XLS" OnClick="ASPxButton1_Click" />
protected void ASPxButton1_Click(object sender, EventArgs e) {
ASPxGridView1.ExportPdfToResponse();
}
Client API
Call the client ExportTo(format) method to export grid data to a file in the specified format.
<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>