Skip to content

DevExpress-Examples/asp-net-mvc-grid-export-selected-columns-in-BeforeExport-event-handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Grid View for ASP.NET MVC - How to use the BeforeExport event to filter the exported columns

This example demonstrates how to export custom (selected) columns of GridView to a PDF file.

Export selected columns to PDF

The BeforeExport event allows you to customize export settings before grid content is exported. You can use this event to hide and show data columns and add custom columns to the exported file.

In this example, a ListBox extension contains a list of grid column names and allows you to select the items. When a user clicks the Export to PDF button, the list of selected column names is sent to the server.

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;
}

On the server, the BeforeExport event handler clears the Columns collection of the exported grid and populates it with the selected columns.

gridVieewSettings.SettingsExport.BeforeExport = (sender, e) => {
    MVCxGridView gridView = sender as MVCxGridView;
    if (sender == null)
        return;
    gridView.Columns.Clear();
    foreach (var name in names) {
        if (string.IsNullOrEmpty(name)) continue;
        gridView.Columns.Add(new MVCxGridViewColumn(name));
    }
};

Files to Review

Documentation

Does this example address your development requirements/objectives?

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