This is a migrated thread and some comments may be shown as answers.

Hide Column When Exporting to Excel in MVC Razor Grid

4 Answers 466 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 09 Sep 2016, 07:51 PM

There is a bound column in my Razor MVC Grid that I do not want exported to Excel.  I have seen some examples that show creating an Excel export event then using hideColumn to remove the column but that does not seem to work for me.  How can I hide a column in the grid export to Excel?

 

.Events(e => e.ExcelExport("excelExport"))

function excelExport(e) {
     e.sender.hideColumn("Received");
}

4 Answers, 1 is accepted

Sort by
0
Accepted
Ianko
Telerik team
answered on 14 Sep 2016, 06:02 AM

Hello John,

 

During ExcelExport event you cannot change the rendering so that to affect the exported content. What you can do with this event is to cancel it and based on predefined flags to export again programmatically and show back the column. You can see the solution here: http://www.telerik.com/forums/export-to-grid-hide-columns#on_CdInsGESYqc5Jumibow

 

Or use similar logic like this one: 

function excelExport(e) {
    if (e.sender._exporting === undefined) {
        e.sender._exporting = true;
    }
    if (e.sender._exporting) {
        e.sender.hideColumn("Received");
        e.preventDefault();
 
        setTimeout(function () {
            e.sender._exporting = false;
            e.sender.saveAsExcel();
        });
    } else {
        e.sender._exporting = true;
        e.sender.showColumn("Received");
    }
}

 

 

Regards,
Ianko
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
John
Top achievements
Rank 1
answered on 14 Sep 2016, 06:19 PM
This works but it has a side effect of screwing up the column widths in my grid.  Instead of my columns taking up the full width of the grid it shrinks them arbitrarily. See attached before and after images.
0
Accepted
Ianko
Telerik team
answered on 15 Sep 2016, 06:58 AM

Hello John,

This is an expected behavior as the inner table is being programmatically set up with different width. 

If you need to retain the 100% width behavior you can use the following line of code after calling hideColumn method.

e.sender.table.css("width", "");

Regards,
Ianko
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
John
Top achievements
Rank 1
answered on 15 Sep 2016, 12:40 PM

In my case it worked when I put it after the hideColumn and showColumn.  Thanks for the help!

 

 

Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Ianko
Telerik team
John
Top achievements
Rank 1
Share this question
or