c# - Issue of addition of blank row in gridview -
i have fetched data db table, bound gridview , export data gridview excel. works fine.
but when added row @ top of gridview in databound event using below code:
protected void gvreports_databound(object sender, eventargs e) { if (gvreports.rows.count > 0) { gridviewrow row = new gridviewrow(0, 0, datacontrolrowtype.header, datacontrolrowstate.normal); int count = gvreports.headerrow.cells.count; tableheadercell cell = new tableheadercell(); cell.borderwidth = 0; cell.text = "commodity " + ddlcommmfarrival.selecteditem.text; cell.columnspan = count - 2; row.controls.add(cell); row.backcolor = colortranslator.fromhtml("#f5fafd"); gvreports.headerrow.parent.controls.addat(0, row); } }
code export gridview excel:
protected void btnexport_click(object sender, eventargs e) { (int = 0; < gvreports.rows.count; i++) { (int j = 0; j < gvreports.rows[i].cells.count; j++) { if (gvreports.rows[i].cells[j].text == "na") { gvreports.rows[i].cells[j].horizontalalign = horizontalalign.right; } } } gvreports.headerrow.cells[1].visible = false; gvreports.headerrow.cells[2].visible = false; string filename = string.empty; int ddlselect = convert.toint32(ddlcommmfarrival.selectedvalue); if (ddlselect == 1) { filename = "marketfee" + datetime.now.tostring().replace("/", "").replace(":", "").replace(" ", "") + ""; } else if (ddlselect == 2) { filename = "commodity arrival" + datetime.now.tostring().replace("/", "").replace(":", "").replace(" ", "") + ""; } //export gridview data ms excel blexporttoexcel blexport = new blexporttoexcel(); //export gridview data ms excel blexport.exportgridviewtoexcel(gvreports, this, filename); } public void exportgridviewtoexcel(gridview grv, system.web.ui.page pg, string filename) { grv.allowpaging = false; pg.response.clear(); pg.response.addheader("content-disposition", "attachment; filename=" + filename + ".xls"); pg.response.charset = ""; pg.response.contenttype = "application/vnd.xls"; system.io.stringwriter stringwrite = new system.io.stringwriter(); system.web.ui.htmltextwriter htmlwrite = new htmltextwriter(stringwrite); grv.rendercontrol(htmlwrite); pg.response.write(stringwrite.tostring()); pg.response.end(); grv.allowpaging = true; }
then adds blank row @ position 0 in excel (export data gridview excel) , truncates last row gridview. knows issue?
try moving headerrow
addition logic out of databound
, rowcreated
. add gridview
declaration:
onrowcreated="gvreports_rowcreated"
and rowcreated
(note datacontrolrowstate.insert
):
protected void gvreports_rowcreated(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.header) { gridviewrow row = new gridviewrow(0, 0, datacontrolrowtype.header, datacontrolrowstate.insert); int count = e.row.cells.count; tableheadercell cell = new tableheadercell(); cell.borderwidth = 0; cell.text = "blah"; cell.columnspan = count - 2; row.controls.add(cell); row.backcolor = colortranslator.fromhtml("#f5fafd"); gvreports.controls[0].controls.addat(0, row); } }
Comments
Post a Comment