Example E3898
Visible to All Users

Grid View for ASP.NET MVC - How to export grid data to different formats

This example illustrates how to use the ExportTo* methods to export the GridView 's content in different formats.

GridView export

Note the following key points when you export GridView extension content:

  • Define the GridView extension in a separate PartialView (TypedListDataBindingPartial.cshtml) without any additional tags.
  • Wrap the GridView's PartialView in a form to apply the client's layout state (sorting, filtering, etc.).
    Code
    @using(Html.BeginForm("ExportTo", "Home")) { // ... @Html.Partial("TypedListDataBindingPartial", Model) }
  • Make a POST request to submit this form to the corresponding Controller Action.
  • The GridViewSettings (especially the Name property) should be the same in PartialView and Controller;
  • The data source/Model should be the same in PartialView and Controller.

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Views/Home/TypedListDataBindingPartial.cshtml
Razor
@Html.DevExpress().GridView(GridViewHelper.ExportGridViewSettings).Bind(Model).GetHtml()
Controllers/HomeController.cs(vb)
C#
using System; using System.Collections.Generic; using System.Web; using System.Web.Mvc; using DevExpress.Web.Mvc; using Export.Models; namespace Export.Controllers { public class HomeController : Controller { public ActionResult Index() { if(Session["TypedListModel"] == null) Session["TypedListModel"] = InMemoryModel.GetTypedListModel(); return View(Session["TypedListModel"]); } public ActionResult TypedListDataBindingPartial() { return PartialView(Session["TypedListModel"]); } public ActionResult ExportTo(string OutputFormat) { var model = Session["TypedListModel"]; switch(OutputFormat.ToUpper()) { case "CSV": return GridViewExtension.ExportToCsv(GridViewHelper.ExportGridViewSettings, model); case "PDF": return GridViewExtension.ExportToPdf(GridViewHelper.ExportGridViewSettings, model); case "RTF": return GridViewExtension.ExportToRtf(GridViewHelper.ExportGridViewSettings, model); case "XLS": return GridViewExtension.ExportToXls(GridViewHelper.ExportGridViewSettings, model); case "XLSX": return GridViewExtension.ExportToXlsx(GridViewHelper.ExportGridViewSettings, model); default: return RedirectToAction("Index"); } } } } public static class GridViewHelper { private static GridViewSettings exportGridViewSettings; public static GridViewSettings ExportGridViewSettings { get { if(exportGridViewSettings == null) exportGridViewSettings = CreateExportGridViewSettings(); return exportGridViewSettings; } } private static GridViewSettings CreateExportGridViewSettings() { GridViewSettings settings = new GridViewSettings(); settings.Name = "gvTypedListDataBinding"; settings.CallbackRouteValues = new { Controller = "Home", Action = "TypedListDataBindingPartial" }; settings.KeyFieldName = "ID"; settings.Settings.ShowFilterRow = true; settings.Columns.Add("ID"); settings.Columns.Add("Text"); settings.Columns.Add("Quantity"); settings.Columns.Add("Price"); return settings; } }
Views/Home/Index.cshtml
Razor
@model System.Collections.IEnumerable <script type="text/javascript"> function OnClick(s, e) { var actionParams = $("form").attr("action").split("?OutputFormat="); actionParams[1] = s.GetMainElement().getAttribute("OutputFormatAttribute"); $("form").attr("action", actionParams.join("?OutputFormat=")); } </script> @using(Html.BeginForm("ExportTo", "Home")) { <table> <tr> <td> @Html.DevExpress().Button(btn => { btn.Name = "btnExportToCSV"; btn.Attributes["OutputFormatAttribute"] = "CSV"; btn.Text = "CSV"; btn.UseSubmitBehavior = true; btn.ClientSideEvents.Click = "OnClick"; }).GetHtml() </td> <td> @Html.DevExpress().Button(btn => { btn.Name = "btnExportToPDF"; btn.Attributes["OutputFormatAttribute"] = "PDF"; btn.Text = "PDF"; btn.UseSubmitBehavior = true; btn.ClientSideEvents.Click = "OnClick"; }).GetHtml() </td> <td> @Html.DevExpress().Button(btn => { btn.Name = "btnExportToRTF"; btn.Attributes["OutputFormatAttribute"] = "RTF"; btn.Text = "RTF"; btn.UseSubmitBehavior = true; btn.ClientSideEvents.Click = "OnClick"; }).GetHtml() </td> <td> @Html.DevExpress().Button(btn => { btn.Name = "btnExportToXLS"; btn.Attributes["OutputFormatAttribute"] = "XLS"; btn.Text = "XLS"; btn.UseSubmitBehavior = true; btn.ClientSideEvents.Click = "OnClick"; }).GetHtml() </td> <td> @Html.DevExpress().Button(btn => { btn.Name = "btnExportToXLSX"; btn.Attributes["OutputFormatAttribute"] = "XLSX"; btn.Text = "XLSX"; btn.UseSubmitBehavior = true; btn.ClientSideEvents.Click = "OnClick"; }).GetHtml() </td> </tr> </table> @Html.Partial("TypedListDataBindingPartial", Model) }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.