I have an embedded gridview in my repeater. On the repeater itemdatabound I'm binding the gridview and trying to retrieve the totals that will be displayed in the gridviewfooters, however, the totals are not working, what am I missing? Please could someone help Cheers
<asp:Repeater ID="rpt" runat="server" onitemdatabound="rpt_ItemDataBound" >
<HeaderTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblName" runat="server" ClientIDMode="Static"></asp:Label>
</td>
<td>
<asp:Label ID="lblAddress" runat="server" ClientIDMode="Static"></asp:Label>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="hfID" runat="server" Value='<%# DataBinder.Eval(Container, "DataItem.link_id") %>' />
<asp:GridView ID="grd" ClientIDMode="Static" runat="server"
Width="100%" DataKeyNames="ID" AutoGenerateColumns="False" GridLines="Vertical"
CellPadding="4" AllowPaging="True" AllowCustomPaging="True" PageSize="25" PagerStyle-Visible="False"
ShowFooter="true" OnRowDataBound="grd_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hfID" runat="server" ClientIDMode="Static" Value='<%# DataBinder.Eval(Container,"DataItem.id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="code" DataField="code" />
<asp:BoundField HeaderText="Description" DataField="desc" />
<asp:BoundField HeaderText="Qty" DataField="quantity" />
<asp:BoundField HeaderText="Employee Paid" DataField="e_total" />
<asp:BoundField HeaderText="Client Charged" DataField="c_total" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:Repeater>
code behind
double dEmpTotal = 0;
double dClientTotal = 0;
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
int ID = int.Parse(((HiddenField)e.Item.FindControl("hfID")).Value);
GridView grd = (GridView)e.Item.FindControl("grd");
DataContext dc = new DataContext(ConfigurationManager.ConnectionStrings["DB"].ConnectionString);
var qry = from p in dc.usp_list_costs(ID)
select p;
List<usp_list_costsResult> lstCosts = new List<usp_list_costsResult>();
lstCosts = qry.ToList();
double dEmpTotal = Convert.ToDouble(lstCosts.Sum(r => r.emp_total));
double dClientTotal = Convert.ToDouble(lstCosts.Sum(r => r.client_total));
grd.DataSource = lstCosts;
grd.DataBind();
}
}
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[4].Text = "Employee Total:" + dEmpTotal;
e.Row.Cells[5].Text = "Client Total:" + dClientTotal;
}
}
I assumed that your totals are always zero, because you try to get dEmpTotal
and dClientTotal
values while handling grd_RowDataBound with row type is Footer, but the values were set to zero at that time.
You have to set the total values when it's binding data to repeater item, in rpt_ItemDataBound
event handler instead.
So you don't need to set Footer rows' value in grd_RowDataBound
method at all.
The code should be like this (Just adding the last two rows as my sample code)
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
int ID = int.Parse(((HiddenField)e.Item.FindControl("hfID")).Value);
GridView grd = (GridView)e.Item.FindControl("grd");
DataContext dc = new DataContext(ConfigurationManager.ConnectionStrings["DB"].ConnectionString);
var qry = from p in dc.usp_list_costs(ID)
select p;
List<usp_list_costsResult> lstCosts = new List<usp_list_costsResult>();
lstCosts = qry.ToList();
double dEmpTotal = Convert.ToDouble(lstCosts.Sum(r => r.emp_total));
double dClientTotal = Convert.ToDouble(lstCosts.Sum(r => r.client_total));
grd.DataSource = lstCosts;
grd.DataBind();
grd.FooterRow.Cells[4].Text = "Employee Total:" + dEmpTotal;
grd.FooterRow.Cells[5].Text = "Client Total:" + dClientTotal;
}
}