Hide Column When Exporting to Excel in MVC Razor Grid

5 posts, 2 answers
  1. John
    John avatar
    19 posts
    Member since:
    Apr 2015

    Posted 09 Sep Link to this post

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

  2. Answer
    Ianko
    Admin
    Ianko avatar
    1535 posts

    Posted 14 Sep Link to this post

    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
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. John
    John avatar
    19 posts
    Member since:
    Apr 2015

    Posted 14 Sep in reply to Ianko Link to this post

    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.
  5. Answer
    Ianko
    Admin
    Ianko avatar
    1535 posts

    Posted 15 Sep Link to this post

    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
  6. John
    John avatar
    19 posts
    Member since:
    Apr 2015

    Posted 15 Sep in reply to Ianko Link to this post

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

     

     

Back to Top
UI for ASP.NET MVC is VS 2017 Ready