c# - Getting total for a column in ListView -


i need sum items in column within listview. put in following code in itemdatabound event, realized after testing it getting bound, oops.

so looking little converting show total column items bound listview.

thanks.

 if (e.item.itemtype == listviewitemtype.dataitem)             {            listviewdataitem item = (listviewdataitem)e.item;                      label lblqty = (label)e.item.findcontrol("lblquantity");            if (lblqty == null)            {                return;            }             if (lblqty.text.length == 0 || lblqty.text == "")            {                return;            }            else            {                listviewtotal += int.parse(lblqty.text);            }        } 

the best method have found implement ondatabinding method control binding. example:

<asp:listview id="listview1" runat="server">     <itemtemplate>         <asp:literal id="yourliteral" runat="server"             ondatabinding="yourliteral_databinding"></asp:literal>     </itemtemplate> </asp:listview> 

first define global counter in .cs:

private int _quantitytotal = 0; 

then implement ondatabinding control:

 protected void yourliteral_databinding(object sender, system.eventargs e)  {      // here value including calling db      // call if want each item being bound.      literal lt = (literal)(sender);      int quantity = (int)(eval("yourquantityfield"));      _quantitytotal += quantity;      lt.text = quantity.tostring();  } 

now have total stored in _quantitytotal can add footer or else after databinding has occurred ondatabound event.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -