This is a migrated thread and some comments may be shown as answers.

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

1 Answer 212 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Martyn
Top achievements
Rank 1
Martyn asked on 16 Feb 2012, 02:35 PM
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

1 Answer, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 17 Feb 2012, 03:45 PM
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 >>
Tags
GridView
Asked by
Martyn
Top achievements
Rank 1
Answers by
Nick
Telerik team
Share this question
or