Current filter:
                                You should refresh the page.
                                We apologize for the inconvenience. Our ordering system and the support center may be offline for a few hours on Sunday, May 19 due to maintenance tasks.

                                How to use the BeforeExport event of a GridView Extension

                                0

                                This example demonstrates how to use the BeforeExport event to export custom (selected) columns of GridView to the PDF file.

                                You must  log in  or  register  to leave comments
                                Select file
                                • BeforeExport.cshtml
                                • BeforeExportPartial.cshtml
                                • HomeController.cs
                                • Northwind.cs
                                Select language
                                • C#
                                • VB.NET
                                Select version
                                • v2011 vol 1.6 - v2012 vol 2.8
                                @using DevExpress.Razor.Models
                                @using System.Web.UI.WebControls
                                @{
                                    ViewBag.Title = "Home Page";
                                }
                                
                                <script type="text/javascript">
                                    function ExportToPDF() {
                                        var names = GetSelectedItemsNames();
                                        if (!names) {
                                            alert("Choose columns to export");
                                            return;
                                        }
                                            
                                        document.getElementById("ExportColumnsNames").value = names;
                                        document.forms[0].submit();
                                    }
                                    function GetSelectedItemsNames() {
                                        var selectedItems = columnNames.GetSelectedValues();
                                        var result = "";
                                        for (var index = 0; index < selectedItems.length; index++) {
                                            result += selectedItems[index] + ";";
                                        }
                                        return result;
                                    }
                                </script>
                                
                                @{
                                    string[] exportColumnsNames = ViewData["ExportColumnsNames"] == null 
                                        ? new string[] { "FirstName", "LastName", "HireDate" } 
                                        : ViewData["ExportColumnsNames"] as string[];
                                }
                                
                                @using (Html.BeginForm("ExportTo", "Home")) {
                                    <div style="padding: 10px;">
                                        <br />
                                        @Html.DevExpress().Button(settings => {
                                            settings.Name = "exportToPdf";
                                            settings.Text = "Export to PDF";
                                            settings.ClientSideEvents.Click = "function(s, e) { ExportToPDF(); }";
                                        }).GetHtml()
                                        @Html.Hidden("ExportColumnsNames", null)
                                    </div>
                                    <div style="float:left;">
                                        @Html.DevExpress().Label(settings => {
                                           settings.Name = "title";
                                           settings.Text = "Export columns: ";
                                        }).GetHtml()
                                    </div>
                                    <div style="float:left; padding-left: 10px; padding-right: 10px;">
                                        @Html.DevExpress().ListBox(settings => {
                                            settings.Name = "columnNames";
                                            settings.Properties.SelectionMode = ListEditSelectionMode.CheckColumn;
                                            settings.Height = Unit.Pixel(225);
                                       
                                            foreach(string name in NorthwindDataProvider.GetColumnsNames()){
                                                settings.Properties.Items.Add(name);
                                                settings.Properties.Items.FindByText(name).Selected = exportColumnsNames.Contains(name);
                                            }
                                        }).GetHtml()
                                    </div>
                                    <div>
                                        @Html.Partial("BeforeExportPartial", NorthwindDataProvider.GetEmployees())
                                    </div>
                                }