“Asyncify” Your WinGrid Exports!

Tom Puglisi / Monday, November 14, 2011

So you have a WinGrid powered application that contains exporting capabilities. This app was very easy to set up so that with the click of a button you can export to Microsoft™ Excel™, Microsoft™ Word™, PDF, or XPS formats. How do you make such awesome functionality even “Awesomer"? By adding the ability to export to these existing formats in an asynchronous fashion!

WinGrid Excel Exporting Async!

Why is this better? Whenever performing any code operation that is part of the user interface thread, your UI will “freeze” or not respond until the operation completes. If you can convert any of these operations to execute outside of the UI thread, your end-user experience can be improved. This does not mean it will be faster, however it means that the perceived performance and application responsiveness will be greatly improved. Bonus points if you even provide your end user with the ability to cancel a currently running operation.

Well, Infragistics has done it again! They have added an incredible feature to their exporting components so that you can choose to export in an asynchronous fashion just by calling the “async” version of the export method! The following exporting components have been given Asynchronous exporting capabilities:

  • WinGridExcelExporter
  • WinGridDocumentExporter
  • WinGridWordWriter

Consider the following simple pattern used to generate a file and then open it. This could be executed at the least from a Button_Click event handler:

view plaincopy to clipboardprint
  1. _theFile = Application.StartupPath + @"\theFile.xlsx";   
  2. this.ultraGridExcelExporter1.Export(this.customersUltraGrid, _theFile);   
  3. System.Diagnostics.Process.Start(_theFile);  

Converting this into an asynchronous operation requires a small change. First we must remove the Process.Start( ) out of this code block. Although this code has worked in my applications for many years and still exists in many production apps, there is a better place to relocate the Process.Start( ) line of code:

view plaincopy to clipboardprint
  1. private void ultraGridExcelExporter1_ExportEnded(   
  2.     object sender, Infragistics.Win.UltraWinGrid.ExcelExport.ExportEndedEventArgs e)   
  3. {   
  4.     System.Diagnostics.Process.Start(_theFile);   
  5. }  

The exporting component, in this case WinGridExcelExporter has an “Export Ended” event. This is going to fire for sure whenever the export has completed.

Also note that I use a class scoped string variable that represents the file name. I used to also declare, set a value, and then use a local string variable to create the file name. If you too are employing that pattern, you should also move it up into a class-scoped variable as I am doing here. My sample code here also uses a silly, hard-coded value; a production application will generate a more elegant file name OR leave it up to your end user with a file save dialog.

The next change to make this operation asynchronous is to call the ExportAsync( ) method from the exporting component:

view plaincopy to clipboardprint
  1. this.ultraGridExcelExporter1.ExportAsync(this.customersUltraGrid, _theFile);  

Now there is just one more thing that depends on how your application is designed. To sum up the next step:


WinGrid and WinGridExcelExporter provide a progress indicator as well as an overlay so that you cannot physically click or interact with the grid. You can even cancel the export process through this intrinsic UI that was built into WinGrid. However, my warning is targeted to those situations where you have OTHER user interface controls that when clicked could be invoking methods on WinGrid. If you do, then you should disable those controls BEFORE the export and enable them AFTER the export.

There you go, this is the info you basically need to convert your synchronous export to an asynchronous export!


Code to export async:

view plaincopy to clipboardprint
  1. private void toolStripButton1_Click(object sender, EventArgs e)   
  2. {   
  3.     //You can also put in code to disable those controls that interact with WinGrid   
  4.     _theFile = Application.StartupPath + @"\theFile.xlsx";   
  6.     this.ultraGridExcelExporter1.ExportAsync(this.customersUltraGrid, _theFile);   
  7. }   
  9. private void ultraGridExcelExporter1_ExportEnded(   
  10.     object sender, Infragistics.Win.UltraWinGrid.ExcelExport.ExportEndedEventArgs e)   
  11. {   
  12.     System.Diagnostics.Process.Start(_theFile);   
  14.     //If you disabled any UI controls that interact with WinGrid, enable them HERE.   
  15. }