c# - nested repeater control and item count of child repeater -
i have designed employee data using nested employee data.i have 2 table emp , dept .table information emp-empid(pk),empname,empjob,empsalary,deptid dept-deptid(fk),deptname
i have shown data in department wise.parent repeater shows department table , child repeater shows emp details.and want count total number of employee department wise total salary.like wise in last want count grand total employee , grand total salary.but problem i'm facing in counting no of employee , salary department wise.here aspx.cs page .. if not getting me what's problem .. see output i've attached 1 screenshot
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.data; using system.data.sqlclient; using system.configuration; public partial class repeatercontrolnested : system.web.ui.page { sqlconnection cn = null; sqlcommand cmd = null; sqldataadapter da = null; dataset ds = null; int totalemployeecount = 0; decimal totalsalary = 0; int grandtotalemployeecount = 0; decimal grandtotalsalary = 0; protected void page_load(object sender, eventargs e) { cn = new [![enter image description here][1]][1]sqlconnection(configurationmanager.connectionstrings["constr"].connectionstring); if (!page.ispostback) { binddept(); } } void binddept() { da = new sqldataadapter("select * dept", cn); ds = new dataset(); da.fill(ds, "dept"); deptrepeater.datasource = ds.tables["dept"]; deptrepeater.databind(); } protected void deptrepeater_itemdatabound(object sender, repeateritemeventargs e) { if (e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem) { repeater emprepeater = (repeater)e.item.findcontrol("emprepeater"); label lbldeptid = (label)e.item.findcontrol("lbldeptid"); label lblstatus = (label)e.item.findcontrol("lblstatus"); da = new sqldataadapter("select e.empid,e.empname,e.empjob,e.empsalary,d.deptname emp e, dept d e.deptid=d.deptid , d.deptid=" + lbldeptid.text, cn); ds = new dataset(); da.fill(ds, "emp"); if (ds.tables["emp"].rows.count > 0) { emprepeater.datasource = ds.tables["emp"]; emprepeater.databind(); } else { lblstatus.text = "no data available"; } } if (e.item.itemtype == listitemtype.footer) { label lblgrandtotalemp = (label)e.item.findcontrol("lblgrandtotalemp"); lblgrandtotalemp.text = grandtotalemployeecount.tostring(); label lblgrandtotalsalary = (label)e.item.findcontrol("lblgrandtotalsalary"); lblgrandtotalsalary.text = grandtotalsalary.tostring("c"); } totalsalary = 0; totalemployeecount = 0; } protected void emprepeater_itemdatabound(object sender, repeateritemeventargs e) { if (e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.item) { label lblempsalary = (label)e.item.findcontrol("lblempsalary"); label lblgrandtotalsalary = (label)e.item.findcontrol("lblgrandtotalsalary"); if (lblempsalary != null) { totalsalary += decimal.parse(lblempsalary.text); totalemployeecount += 1; grandtotalemployeecount += 1; grandtotalsalary = grandtotalsalary + decimal.parse(lblempsalary.text); } } if (e.item.itemtype == listitemtype.footer) { label lbltotalemp = (label)e.item.findcontrol("lbltotalemp"); lbltotalemp.text = totalemployeecount.tostring(); label lbltotalsalary = (label)e.item.findcontrol("lbltotalsalary"); lbltotalsalary.text = totalsalary.tostring("c"); } } }
here design page -
<%@ page language="c#" autoeventwireup="true" codefile="repeatercontrolnested.aspx.cs" inherits="repeatercontrolnested" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table border="1" style="background-color:blue;color:white" width="100%"> <tr> <th align="center">employee data</th> </tr> </table> </div> <div> <asp:repeater id="deptrepeater" runat="server" onitemdatabound="deptrepeater_itemdatabound"> <itemtemplate> <table width="100%"> <tr style="background-color:#2bb9d9;color:white"> <td align="left"> dept name : <%#eval("deptname") %> <asp:label id="lbldeptid" runat="server" style="display:none" text='<%#eval("deptid") %>'></asp:label> </td> </tr> </table> <asp:repeater id="emprepeater" runat="server" onitemdatabound="emprepeater_itemdatabound"> <headertemplate> <table width="100%"> <tr style="background-color:yellow;color:green"> <td width="20%">emp id</td> <td width="20%">emp name</td> <td width="20%">emp job</td> <td width="20%">emp salary</td> <td width="20%">dept name</td> </tr> </table> </headertemplate> <itemtemplate> <table width="100%"> <tr> <td width="20%"> <%#eval("empid") %> </td> <td width="20%"> <%#eval("empname") %> </td> <td width="20%"> <%#eval("empjob") %> </td> <td width="20%"> <asp:label id="lblempsalary" runat="server" text='<%#eval("empsalary") %>'></asp:label> </td> <td width="20%"> <%#eval("deptname") %> </td> </tr> </table> </itemtemplate> <footertemplate> <table width="100%"> <tr style="background-color:yellow;color:green"> <th width="40%" colspan="2" align="left"> total no of employee: <asp:label id="lbltotalemp" runat="server"></asp:label> </th> <th width="20%" align="right"> total salary: </th> <th width="40%" colspan="2" align="left"> <asp:label id="lbltotalsalary" runat="server"></asp:label> </th> </tr> </table> </footertemplate> </asp:repeater> <asp:label id="lblstatus" runat="server" backcolor="red" forecolor="white"></asp:label> </itemtemplate> <footertemplate> <table width="100%"> <tr style="background-color:yellow;color:green"> <th width="40%" colspan="2" align="left"> grandtotal of employee: <asp:label id="lblgrandtotalemp" runat="server"></asp:label> </th> <th width="20%" align="right"> grand total salary: </th> <th width="40%" colspan="2" align="left"> <asp:label id="lblgrandtotalsalary" runat="server"></asp:label> </th> </tr> </table> </footertemplate> </asp:repeater> </div> </form>
enter code here
only mistake doing in statement
if (e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.item) instead should write
if (e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem) now working
Comments
Post a Comment