I have a fully working GridView
using DevExpress
with MVC 5
.
I would like to add the possibility to export the data in various formats, and I've managed to do that by following some online examples.
Now, I need to hide some columns when exporting, and I've read about the BeforeExport
property.
Here's what I've done:
var grid = Html.DevExpress().GridView(settings => {
//blah blah stuff
#region Export
settings.Toolbars.Add(t => {
t.EnableAdaptivity = true;
t.Items.Add(GridViewToolbarCommand.ExportToXls);
t.Items.Add(GridViewToolbarCommand.ExportToXlsx);
t.Items.Add(GridViewToolbarCommand.ExportToCsv);
});
settings.SettingsExport.EnableClientSideExportAPI = true;
settings.SettingsExport.ExcelExportMode = DevExpress.Export.ExportType.DataAware;
settings.SettingsExport.RenderBrick = (sender, e) => {
if (e.RowType == GridViewRowType.Data && e.VisibleIndex % 2 == 0)
e.BrickStyle.BackColor = System.Drawing.Color.FromArgb(0xEE, 0xEE, 0xEE);
};
settings.SettingsExport.Landscape = true;
settings.SettingsExport.BeforeExport = (sender, e) => {
MVCxGridView gridView = sender as MVCxGridView;
if (sender == null) return;
gridView.Columns["myColumnFieldNameToHide"].Visible = false;
};
#endregion
//blah blah other stuff
}
The thing is, no matter what, the callback I've set for BeforeExport
is never executed, my guess is the event is never fired.
What could it be?
A similar question was discussed in the official DevExpress ticket.
You can find a workaround for this scenario: Handle the ToolbarItemClick event and change the visibility of the necessary columns:
settings.ToolbarItemClick += (s, e) =>
{
var gridView = s as MVCxGridView;
if(gridView == null)
return;
if(e.Item.Command == GridViewToolbarCommand.ExportToXlsx) {
gridView.Columns["Text"].Visible = false;
}
};