Async Export
Telerik DataGrid supports export to PDF, Excel and CSV formats. The control allows also asynchronous export which executes on a background thread, thus avoid freezing the main thread.
The async export supports the same features as the standard export, but it provides few additional events and also it requires slightly different output stream management. To use this feature, call the ExportAsync
method of RadDataGrid
.
Using the ExportAsync method
string projectRootFolderPath = System.IO.Path.GetFullPath("../../../../../../", AppDomain.CurrentDomain.BaseDirectory);
FileStream stream = File.Open(projectRootFolderPath + "\" + "SampleDocument.xlsx", FileMode.OpenOrCreate);
this.dataGrid.ExportAsync(stream, ExportFormat.Xlsx);
Stream
object provided to the stream
parameter of the method will be automatically closed and disposed when the export operation finishes.
This type of export reports progress. The AsyncExportProgressChanged
event of RadDataGrid
is raised each time a row is exported. The event can be used to check the current progress and display additional information to the user, if needed.
Listening for exporting progress changed
private void RadDataGrid_AsyncExportProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
int progress = e.ProgressPercentage;
}
AsyncExportCompleted
event of RadDataGrid
is raised when the export finishes. The event arguments contain information about the export state (was it cancelled and is there an error).
Checking the export for errors
private void dataGrid_AsyncExportCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
bool cancelled = e.Cancelled;
Exception? error = e.Error;
}