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.
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 | Export |
Export to RTF | Export |
Export to DOCX | Export |
Export to CSV | Export |
Export to XLS | Export |
Export to XLSX | Export |
<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 | Export |
Export |
DOCX | Export |
Export |
Export |
Export |
|
RTF | Export |
Export |
XLS | Export |
Export |
XLSX | Export |
Export |
<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>