Friday, May 24, 2013

merge two datatable and sum the data and bind into gridview

  MergeDT.Merge(ds.Tables[1]);
MergeDT.Merge(ds.Tables[2]);

 protected void CreateTotalTarget(DataTable MergeDT)
    {
        if (MergeDT != null)
        {
            if (MergeDT.Rows.Count > 0)
            {
                DataTable PrincipalDT = new DataTable();
                DataView dv = new DataView(MergeDT);
                DataTable FinalDT = MergeDT.Clone();
                PrincipalDT = dv.ToTable(true, "PrincipalName");
                for (int i = 0; i < PrincipalDT.Rows.Count; i++)
                {
                    object ISGQ1TgT;
                    ISGQ1TgT = MergeDT.Compute("SUM([ISG Q1 Trgt])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");
                    object ISGQ1Ach;
                    ISGQ1Ach = MergeDT.Compute("SUM([ISG Q1 Ach])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");

                    object ISGQ2TgT;
                    ISGQ2TgT = MergeDT.Compute("SUM([ISG Q2 Trgt])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");
                    object ISGQ2Ach;
                    ISGQ2Ach = MergeDT.Compute("SUM([ISG Q2 Ach])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");

                    object ISGQ3TgT;
                    ISGQ3TgT = MergeDT.Compute("SUM([ISG Q3 Trgt])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");
                    object ISGQ3Ach;
                    ISGQ3Ach = MergeDT.Compute("SUM([ISG Q3 Ach])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");

                    object ISGQ4TgT;
                    ISGQ4TgT = MergeDT.Compute("SUM([ISG Q4 Trgt])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");
                    object ISGQ4Ach;
                    ISGQ4Ach = MergeDT.Compute("SUM([ISG Q4 Ach])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");

                    object Annual_Trgt;
                    Annual_Trgt = MergeDT.Compute("SUM([Annual_Trgt])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");
                    object Annual_Ach;
                    Annual_Ach = MergeDT.Compute("SUM([Annual_Ach])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");

                    DataRow drow = null;
                    drow = FinalDT.NewRow();
                    drow["PrincipalName"] = PrincipalDT.Rows[i]["PrincipalName"].ToString();
                    drow["ISG Q1 Trgt"] = ISGQ1TgT;
                    drow["ISG Q1 Ach"] = ISGQ1Ach;
                    drow["ISG Q2 Trgt"] = ISGQ2TgT;
                    drow["ISG Q2 Ach"] = ISGQ2Ach;
                    drow["ISG Q3 Trgt"] = ISGQ3TgT;
                    drow["ISG Q3 Ach"] = ISGQ3Ach;
                    drow["ISG Q4 Trgt"] = ISGQ4TgT;
                    drow["ISG Q4 Ach"] = ISGQ4Ach;
                    drow["Annual_Trgt"] = Annual_Trgt;
                    drow["Annual_Ach"] = Annual_Ach;

                    FinalDT.Rows.InsertAt(drow, 1);
                  
                }
                DataView _dv = new DataView(FinalDT);
                _dv.Sort = "PrincipalName ASC";
                gvTotalTarget.DataSource = _dv;
                gvTotalTarget.DataBind();
            }
        }
   
    }