Exporting GridView (with DataPager) to Excel and getting all rows (with MVVM)

2 posts, 0 answers
  1. Martyn
    Martyn avatar
    18 posts
    Member since:
    Mar 2011

    Posted 16 Feb 2012 Link to this post

    Hi,

    I have a RadGridView with data driven from a RadDataPager and am working on having the data exported to Excel. Using MVVM I have a button that fires a command in the UserControls View Model with the command parameter set as the RadDataGrid, this performs the export (below). This is fine and exporting the current page of data works well, however issues occur when trying to export the whole dataset.

    Looking around I have seen normally the solution to this is to change the DataPager PageSize to 0 before export, however this is an approach that hasn't worked for me with MVVM. I set the pageSize using a binding to the viewmodel, and was hoping changing the value would be as simple as follows;

    protected void ExportExcelClicked(object o)
    {
        PageSize = 0;
        RadGridView grid = o as RadGridView;
     
     
        string extension = "csv";
        SaveFileDialog dialog = new SaveFileDialog()
        {
            DefaultExt = extension,
            Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "Excel"),
            FilterIndex = 1
        };
        if (dialog.ShowDialog() == true)
        {
            using (Stream stream = dialog.OpenFile())
            {
                grid.Export(stream,
                 new GridViewExportOptions()
                 {
                     Format = ExportFormat.Csv,
                     ShowColumnHeaders = true,
                     ShowColumnFooters = true,
                     ShowGroupFooters = false,
                 });
            }
        }
     
        //reset page size
        PageSize = PAGESIZE;
    }

    However this doesn't change the value of Object o (i guess maybe a copy is made instead of passing the object reference?), I also tried directly setting the grids ItemsSource to my full datasource, but this doesn't seem to work either - On debugging the grid correctly has an ItemsSource with the right number of elements, this just doesn't seem to translate into being exported to Excel (do I need to do some kind of refresh on the data?)

    I also tried using RowVirtualization to avoid using the DataPager completely, but this was incredibly slow (the window hung for about a minute then when rows were loaded scrolling was very choppy - suggesting it wasn't working correctly.)

    I'm wondering if you could provide a best practice solution to this problem :)

    Other info;
    * Data passed in by binding (ObservableCollection)
    * MVVM

    Thanks
  2. Nick
    Admin
    Nick avatar
    593 posts

    Posted 17 Feb 2012 Link to this post

    Hi Martyn,

    The best and probably only solution to the problem, is to set the PageSize to 0, but I see that you already know that. Please look at the attached sample to see how you can achieve the functionality using MVVM. 

    As for setting the ItemsSource of the GridView, have you tried Rebinding it after the Source change? 

    Hope this helps! 

    Greetings,
    Nik
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top