c# - How to export all pages of gridview to excel without columns containing controls? -


i have gridview has 2 columns dropdownlist , upload control. trying export gridview excel , code works works export current view not pages. when try export pages data binding fails , code crashes. please if know way of getting pages exported excel/csv or without columns containing controls.

<asp:imagebutton id="exportbtn" runat="server" imageurl="../images/excelicon.jpg" alt="export" width="40px" height="40px" style="border-width:0px" onclick="exportbtn_click" />   protected void exportbtn_click(object sender, imageclickeventargs e) {          gv.columns[7].visible = false;         gv.columns[8].visible = false;          response.clearcontent();         response.appendheader("content-disposition", "attachment; filename=documents.xls");         response.contenttype = "application/excel";         gv.allowpaging = false;         gv.databind();          stringwriter sw = new stringwriter();         htmltextwriter htw = new htmltextwriter(sw);          response.charset = string.empty;          gv.rendercontrol(htw);         response.write(sw.tostring());          response.end();     } 

the error when try export pages is: server error in '...20150605_0846_local' application.

'docstatusdropdown' has selectedvalue invalid because not exist in list of items. parameter name: value

description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code.

exception details: system.argumentoutofrangeexception: 'docstatusdropdown' has selectedvalue invalid because not exist in list of items. parameter name: value  source error:    line 194:        gvmaingrid.columns[8].visible = false; line 195:        gvmaingrid.allowpaging = false; line 196:        gvmaingrid.databind(); line 197:        response.clearcontent(); line 198:        response.appendheader("content-disposition", "attachment; filename=missingdocuments.xls"); 

using excellibrary it's pretty easy achieve it. needs pass dataset createworkbook method.

    protected void createexcel_click(object sender, eventargs e)      {          list<student> datasource = (list<student>)grddata.datasource;           dataset ds = new dataset("newdataset");           datatable dtable = extentionhelper.todatatable<student>(datasource);          ds.tables.add(dtable);          excellibrary.datasethelper.createworkbook("c://myexcelfile.xlsx", ds);       } 

extentionhelper datatable list<student>

public static class extentionhelper     {         public static datatable todatatable<t>(this ienumerable<t> collection)         {             datatable dt = new datatable("datatable");             type t = typeof(t);             propertyinfo[] pia = t.getproperties();              //inspect properties , create columns in datatable             foreach (propertyinfo pi in pia)             {                 type columntype = pi.propertytype;                 if ((columntype.isgenerictype))                 {                     columntype = columntype.getgenericarguments()[0];                 }                 dt.columns.add(pi.name, columntype);             }              //populate data table             foreach (t item in collection)             {                 datarow dr = dt.newrow();                 dr.beginedit();                 foreach (propertyinfo pi in pia)                 {                     if (pi.getvalue(item, null) != null)                     {                         dr[pi.name] = pi.getvalue(item, null);                     }                 }                 dr.endedit();                 dt.rows.add(dr);             }             return dt;         }     } 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -