嵌套Gridview,祖父母,父母,孩子

时间:2018-09-26 19:18:03

标签: c# asp.net parent-child accounting nested-gridview

帮我解决这个问题,我想显示父行余额列上的子行余额之和和父行余额列上的余额对父行的总和,

问题是当我使用foreach循环获取余额时,它在没有余额的行上复制了余额。

    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 个答案:

答案 0 :(得分:0)

每个嵌套的gridview的rowdatabound完成后,您需要重置为零。在重置为零之前,您可以将总余额添加到父gridview的余额中。