0

help me with this, i want to show the sum of child row balance on parent row balance column and sum of parent row on balance on grand parent row balance column,

Problem is when i use foreach loop for getting the balance it duplicating the balance,on the rows that do not have balance.

    protected void gvCategory_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        GridView gvType = (GridView)sender;
        string CategoryId = gvType.DataKeys[e.Row.RowIndex].Value.ToString();
        GridView GvAccount = e.Row.FindControl("gvType") as GridView;
        Label lblCurrentBalance = e.Row.FindControl("lblCCurrentBalance") as Label;
        GvAccount.DataSource = GetData(string.Format("select * from COA_Type where SubCategoryId='{0}'", CategoryId));
        GvAccount.DataBind();
        foreach (GridViewRow row in GvAccount.Rows)
        {
            if (row.Cells[3].Text != null)
            {
                NatureBalance += Convert.ToDecimal(((Label)row.Cells[3].FindControl("lblTCurrentBalance") as Label).Text);
            }
            lblCurrentBalance.Text = NatureBalance.ToString();
        }

     }
}

protected void gvType_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        GridView gvAccount = (GridView)sender;
        Label lblCurrentB = e.Row.FindControl("lblTCurrentBalance") as Label;
        string TypeId = gvAccount.DataKeys[e.Row.RowIndex].Value.ToString();
        GridView GvAccount = e.Row.FindControl("GvAccount") as GridView;
        DtAc = obj.GetClientAccounts(Convert.ToInt32(Session["ClientId"]), Convert.ToInt32(TypeId));
        CategoryBalance = DtAc.AsEnumerable().Sum(row => row.Field<decimal>("CurrentBalance"));
        lblCurrentB.Text = CategoryBalance.ToString();

        GvAccount.DataSource = DtAc;
        GvAccount.DataBind();
    }
}

1 Answers1

0

You need to reset to zero after each nested gridview's rowdatabound is finished. Before reset to zero, you can add sum balance into the balance of parent gridview.

Mike
  • 721
  • 7
  • 13