This example demonstrates how to export custom (selected) columns of GridView to a PDF file.
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));
}
};
(you will be redirected to DevExpress.com to submit your response)