MVVM, Infragistics XamDataGrid, and Exporting to Excel

I was watching Craig Shoemaker’s video blog post on Introduction to XamDataGrid Excel Exporting and was excited that he started things out with a ViewModel but the export code was all performed in a code-behind. This left me wanting more. The problem is that InfragisticsDataPresenterExcelExporter requires you to pass a DataPresenterBase object (implemented by the XamDataGrid) as a parameter. This makes sense as the power of the DataPresenterExcelExporter feature is that whatever formatting you have put in place on your grid is what gets exported, saving me being asked to try and replicate a user’s grouping, sorting, or what-not in Excel.
 
So, I used a UserControl, and PRISM‘s generic DelegateCommand (one of my favorites) to pass the XamDataGrid instance through to my ViewModel and was able to move toward a more pure MVVM implementation of the export to excel feature.
 
 You can find the complete sample here:

 

Advertisements

6 thoughts on “MVVM, Infragistics XamDataGrid, and Exporting to Excel

  1. one concern here: the above solution is only good for cases where export button and grid are in the same view. How would I do if Export button is in say different view(like menuitem of container shell).
    If button and grid are in same view then we can achieve the same without involving the viewModel since it would be within view.

    • In the case that you mention, where there is a need for inter-viewmodel communication, I have always resorted to using event subscription/publication processes. Prism, MVVMLight, and Caliburn all have an eventing framework available for just such a scenario.

      At least that’s what I do.

      HTH,
      JH

    • Additionally, as soon as you are putting functionality withing the view (the button click for an export as well as the grid) and then handling the export within the view, you’ve now introduced an surface area within your code that is not easily testable through unit-testing means. The driving force behind all of the predominant UI technologies today (MVC, MVP, MVVM) is a focus on testability and the benefits that come with those patterns.

  2. Hi Hunt, I’ve just read your post and wanted to take a look at your code but the link does not work. Could you fix it, please?

  3. Apparently the file was removed from skydrive… I’ve posted an updated sample. It has a lot more in it than the original I posted so you’ll have to sift through it.

  4. Great, thanks a lot!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s