c# - GridvView FooterRow Total -
i have gridview dynamically generated columns based on user's query. means can have 1 column xxx column name, or can have 4 columns.
so: () means optional
aaa | (bbb) | (ccc) | (ddd) 1 7 45 2 22 9 6 33 ... ... ... ...
i need sum totals of each of columns without knowing columns available until program runs.
i'm trying use e.row.rowtype == datacontrolrowtype.footer portion of gridview_rowdatabound event, can't seem figure out how work.
i saving running totals in variables via e.row.rowtype == datacontrolrowtype.datarow portion of event, can't seem figure out how "inject" saved items footer of grid based on available column(s).
can give me bit of assistance?
edit
gridview done basic mark since columns built dynamically.
<asp:gridview id="gv" runat="server" showfooter="true" nrowdatabound="gv_rowdatabound"> </asp:gridview>
and code columns:
private void bindgrid() { datatable dt = new datatable(); var stt = t in edcquery.tolist() t.technician.teamid == 1 orderby t.requesttype.name select new { requesttype = t.requesttype.name, tech = t.technician.name, }; dt.columns.add("support ticket type"); datarow dr; foreach (var col in stt.orderby(x => x.tech).groupby(x => x.tech)) { dt.columns.add(col.key.substring(0, col.key.indexof(' ')).trim()); } foreach (var type in stt.groupby(x => x.requesttype)) { dr = dt.newrow(); dr["support ticket type"] = type.key; foreach (var tech in type.groupby(x => x.tech)) { dr[tech.key.substring(0, tech.key.indexof(' ')).trim()] = (object)tech.count() == system.dbnull.value ? 0 : tech.count(); } dt.rows.add(dr); } //gvedcsupporttickettype.footerrow.cells[2].text = "0"; gvedcsupporttickettype.datasource = dt; gvedcsupporttickettype.databind(); } double atot = 0.0; double btot = 0.0; double ctot = 0.0; double dtot = 0.0; protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { var datarow = (datarowview)e.row.dataitem; string[] columnnames = { "aaa", "bbb", "ccc", "ddd" }; foreach (var item in columnnames) { var checkname = datarow.row.table.columns.cast<datacolumn>().any(x => x.columnname.equals(item, stringcomparison.invariantcultureignorecase)); if (checkname) { if (databinder.eval(e.row.dataitem, item) != dbnull.value && convert.toint32(databinder.eval(e.row.dataitem, item)) != 0) { switch (item) { case "aaa": atot += convert.toint32(databinder.eval(e.row.dataitem, item)); break; case "bbb": btot += convert.toint32(databinder.eval(e.row.dataitem, item)); break; case "ccc": ctot += convert.toint32(databinder.eval(e.row.dataitem, item)); break; case "ddd": dtot += convert.toint32(databinder.eval(e.row.dataitem, item)); break; } } } } } else if (e.row.rowtype == datacontrolrowtype.footer) { e.row.cells[0].text = "totals:"; e.row.cells[0].attributes.add("style", "text-align: right;"); } }
so can see, builds grid datatable , uses columns necessary @ run time. there no static footertemplate or anything.
i think should use try catch below.
else if (e.row.rowtype == datacontrolrowtype.footer) { try { e.row.cells[0].text = "totals:" + atot.tostring(); e.row.cells[0].attributes.add("style", "text-align: right;"); } catch { } try { e.row.cells[1].text = "totals:" + btot.tostring(); e.row.cells[1].attributes.add("style", "text-align: right;"); } catch { } try { e.row.cells[2].text = "totals:" + ctot.tostring(); e.row.cells[2].attributes.add("style", "text-align: right;"); } catch { } try { e.row.cells[3].text = "totals:" + dtot.tostring(); e.row.cells[3].attributes.add("style", "text-align: right;"); } catch { } }
or
else if (e.row.rowtype == datacontrolrowtype.footer) { int = 0; foreach (tablecell c in e.row.cells) { switch (i) { case 0: c.text = "totals:" + atot.tostring(); c.attributes.add("style", "text-align: right;"); break; case 1: c.text = "totals:" + btot.tostring(); c.attributes.add("style", "text-align: right;"); break; case 2: c.text = "totals:" + ctot.tostring(); c.attributes.add("style", "text-align: right;"); break; case 3: c.text = "totals:" + dtot.tostring(); c.attributes.add("style", "text-align: right;"); break; } i++; } }
so if there footer cell available , go through otherwise go catch part.
Comments
Post a Comment