This example illustrates how to use the ExportTo* methods to export the GridView 's content in different formats.
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
- HomeController.cs (VB: HomeController.vb)
- Index.cshtml (VB: Index.vbhtml)
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
Razor@Html.DevExpress().GridView(GridViewHelper.ExportGridViewSettings).Bind(Model).GetHtml()
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;
}
}
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)
}