asp.net - Sum column values and show right below the column using label -


this gridview design.

 <asp:gridview id="gridview1" runat="server" horizontalalign="center" cellpadding="3"                 gridlines="both" showfooter="true" cssclass="contentfont">                 <rowstyle wrap="false" />                                     <headerstyle wrap="false" height="25px" />                 <footerstyle forecolor="black" backcolor="lightgray" />             </asp:gridview> 

after binding columns db grdiview has around 8 columns. need sum columns total values , show right below column using labels. can add footerrow label if add, delete, edit , update functionality required. since gridview readonly , no updation required, confused how achieve this.

query:

i using query bind grid. need sum columns except first 2 in select query.

command.commandtext = "select appname, year, revisedvalue, salesprofit, variance, monthlyprofit, quarterprofit, yearlyprofit reports  

can me solve this.

you not specifying how store data, i'll go datatable, called dtable in example.

add following ode markup:

<asp:gridview id="gridview1" /* .... */ onrowdatabound="gridview1_rowdatabound">                 </asp:gridview> 

edit here edited post, change code tested in sample project. used adventureworks database sample data. make quick used linq-to-entites data , bind gridview. after used above mentioned onrowdatabound="gridview1_rowdatabound calculate footer values.

    void page_load(object sender, eventargs e)     {         if (!ispostback)         {             using (var context = new adventureworks2008r2entities())             {                 var result = context.products.where(p => p.listprice != 0)                                     .select(x => new                                     {                                         name = x.name,                                         listprice = x.listprice,                                         standardcost = x.standardcost                                     })                                     .take(5);                 gridview1.datasource = result.tolist();                 gridview1.databind();             }         }      }      protected void gridview1_rowdatabound(object sender, gridviewroweventargs e)     {         using (var context = new adventureworks2008r2entities())         {             var result = context.products.where(p => p.listprice != 0)                                 .select(x => new                                 {                                     name = x.name,                                     listprice = x.listprice,                                     standardcost = x.standardcost                                 })                                 .take(5);             if (e.row.rowtype == datacontrolrowtype.footer)             {                 e.row.cells[1].text = result.tolist().sum(x => x.listprice).tostring();                 e.row.cells[2].text = result.tolist().sum(x => x.standardcost).tostring();             }            }     } 

output:

gridview calculated footer

*note: * code far optimal, because querying database twice. please don't use in productive code. better data, store in database , calculate sum() of needed fields in memory.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -