RadGrid for ASP.NET AJAX

RadControls Send comments on this topic.
Overview
Controls > RadGrid > RadGrid export > Overview

Glossary Item Box

Supported formats

Telerik RadGrid can export your data to the following formats:
Format Sub-format Inner type Command name Predefined constants Server-side method Client-side method
PDF n/a binary ExportToPdf RadGrid.ExportToPdfCommandName ExportToPdf() ExportToPdf()
Excel HTML HTML ExportToExcel RadGrid.ExportToExcelCommandName ExportToExcel() ExportToExcel()
Excel ExcelML XML ExportToExcel RadGrid.ExportToExcelCommandName ExportToExcel() ExportToExcel()
Word n/a HTML ExportToWord RadGrid.ExportToWordCommandName ExportToWord() ExportToWord()
CSV n/a text ExportToCsv RadGrid.ExportToCsvCommandName ExportToCSV() ExportToCsv()

Common properties and events

In addition to the export format's specific properties, the ExportSettings group exposes several common properties:

  • ExportOnlyData

As the name says, this property is helpful when you want to export only the data - e.g. to exclude the controls from the exported file.

  • IgnorePaging

When you enable IgnorePaging, RadGrid will rebind before export in order to fetch all the data from your datasource.

  • OpenInNewWindow

By default, the exported file will be handled by the program associated with the appropriate file type. If you prefer to give the user the option to choose whether to save, open (inline) or cancel you should enable this property. 

Even if you set OpenInNewWindow="false", that doesn't guarantee that the file will be opened inside the browser window. The way the exported file will be displayed inline depends on the OS/browser settings. The end-user could manage the file extensions with programs like NirSoft's FileTypesMan. For browsers, other than Internet Explorer, you should use the built-in settings.
  • FileName

This is helpful when you want to give a predefined name for your file. Please note that the file name can't be longer than 256 symbols.

Unicode names are not supported out-of-the-box for Internet Explorer 6 and 7. Of course you can manually encode the file name and it will be shown properly in the "Save" dialog (OpenInNewWindow="true"). 

HttpUtility.UrlEncode("unicode string", System.Text.Encoding.UTF8);


Internet Explorer ignores the FileName property when OpenInNewWindow is set to false.
  • HideStructureColumns

Removes the structure columns - GridRowIndicatorColumn, GridExpandColumn as well as the first GridGroupSplitterColumn.

[ASP.NET] ExportSettings group Copy Code
<ExportSettings
   
HideStructureColumns="true"
   
ExportOnlyData="true"
   
IgnorePaging="true"
   
OpenInNewWindow="true">
</
ExportSettings>

You can receive this error if the file name is longer than 256 symbols:

'<filename>.<extension>' could not be found. Check the spelling of the file name, and verify that the file location is correct.

OnGridExporting event


This event is useable in many scenarios when you want to modify the output file - for example you may want to add some custom information above RadGrid or when you want to remove/add/replace parts of the content. The only limitation applies to the PDF export because by the time the OnGridExporting event is raised, the PDF is already generated. For more information, please visit the dedicated PDF format topic which introduces the OnPdfExporting event that is designed specifically for this format.

 

Below is the barebone logic for both common RadGrid exporting events:

C# Copy Code
protected void RadGrid1_GridExporting(object source, GridExportingArgs e)
{
   
switch (e.ExportType)
   {
        
case ExportType.Excel:
            
//do something with the e.ExportOutput value
            
break;
        
case ExportType.ExcelML:
            
//do something with the e.ExportOutput value
            
break;
        
case ExportType.Word:
            
//do something with the e.ExportOutput value
            
break;
        
case ExportType.Csv:
            
//do something with the e.ExportOutput value
            
break;
        
case ExportType.Pdf:
            
//you can't change the output here - use the PdfExporting event instead
            
break;
   }
}

 

VB.NET Copy Code
Protected Sub RadGrid1_GridExporting(source As Object, e As GridExportingArgs)
   Select Case e.ExportType
      Case ExportType.Excel
          'do something with the e.ExportOutput value
          Exit Select
      Case ExportType.ExcelML
          'do something with the e.ExportOutput value
          Exit Select
      Case ExportType.Word
          'do something with the e.ExportOutput value
          Exit Select
      Case ExportType.Csv
          'do something with the e.ExportOutput value
          Exit Select
      Case ExportType.Pdf
          'you can't change the output here - use the PdfExporting event instead
          Exit Select
   End Select
End Sub

Common scenarios

Exporting with client-side binding is not supported, RadGrid should be bound server-side, otherwise you will receive an empty file.
  • Export with custom paging

When you have custom paging enabled for your grid, you need to set the PageSize property of the grid to be equal to the VirtualItemCount in order to export all records successfully with IgnorePaging set to true. Here is an example:

C# Copy Code
protected void Button1_Click(object sender, EventArgs e)
{
    RadGrid1.PageSize = RadGrid1.MasterTableView.VirtualItemCount;
    RadGrid1.ExportSettings.IgnorePaging = true;
    RadGrid1.ExportSettings.OpenInNewWindow = true;
    RadGrid1.MasterTableView.ExportToExcel(); 
}

 

VB.NET Copy Code
Protected Sub Button1_Click(ByVal sender as Object, ByVal e as EventArgs)
    RadGrid1.PageSize = RadGrid1.MasterTableView.VirtualItemCount
    RadGrid1.ExportSettings.IgnorePaging = True
    RadGrid1.ExportSettings.OpenInNewWindow = True
    RadGrid1.MasterTableView.ExportToExcel()
End Sub
  • Export from AJAX-enabled RadGrid

The exporting feature works only with regular postbacks. This means, that the asynchronous postback should be canceled when performing an export.

More information on this topic is available below:

Export from ajaxified grid

Exclude controls from ajaxifying

Export RadGrid content to Excel/Word/CSV/PDF with Ajax enabled 

  • Nested grids / Exporting multiple RadGrids

The following code-library project demonstrates how to export nested RadGrids to PDF/Excel (HTML)

Export multiple RadGrids in single PDF/Excel file

  • Export with client-side binding

RadGrid doesn't support exporting with client-side binding. To be able to export in such scenario, you should bind RadGrid on the server.

  • Export over SSL

You might receive the following error message when using the export feature over SSL and Internet Explorer:

"Internet Explorer cannot download 'file' from 'server'.
Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later."

 

In order to prevent this error add the following lines just before the exporting:

C# Copy Code
RadGrid1.Page.Response.ClearHeaders();
RadGrid1.Page.Response.Cache.SetCacheability(HttpCacheability.Private);
 
VB.NET Copy Code
RadGrid1.Page.Response.ClearHeaders()
RadGrid1.Page.Response.Cache.SetCacheability(HttpCacheability.[Private])

 

Obsolete methods

The following methods are marked as obsolete as of RadGrid v4.6 (part of RadControls for ASP.NET)

  • server-side
    ExportToWord (fileName, dataOnly, ignorePaging)
    ExportToExcel (fileName, dataOnly, ignorePaging)

    ExportToWord2007 (fileName, dataOnly, ignorePaging)
    ExportToExcel2007 (fileName, dataOnly, ignorePaging)
  • client-side
    ExportToWord (fileName)
    ExportToExcel (fileName)

 

For real-life example, review our online demo.