2016-02-16 80 views
0

我使用的是通過一個XML文件中導入合併數據列C#

所以我合併多個數據表到一個數據表,並獲得具有相同的代理名稱行和每列中添加其值的數據表在一個數據行,並把它放到一個新的表,並在數據網格視圖中顯示它 例

輸入:

Agent Product1 Product2 
    A  1   3 
    B  4   5 
    A  5   4 

輸出:

Agent Product1 Product2 
    A  6   7 
    B  4   5 

回答

0

希望這是什麼youre尋找:)只要通過你的數據表來DT,你會發現你需要輸出dtFinal

 DataTable dt = new DataTable(); 
     dt.Columns.Add("Agent"); 
     dt.Columns.Add("Product1"); 
     dt.Columns.Add("Product2"); 
     DataRow dr = dt.NewRow(); 
     dr[0] = "AA"; 
     dr[1] = 7; 
     dr[2] = 5; 
     dt.Rows.Add(dr); 

     dr = dt.NewRow(); 
     dr[0] = "AA"; 
     dr[1] = 5; 
     dr[2] = 6; 
     dt.Rows.Add(dr); 

     dr = dt.NewRow(); 
     dr[0] = "BB"; 
     dr[1] = 2; 
     dr[2] = 3; 
     dt.Rows.Add(dr); 




DataView view = new DataView(dt); 
     DataTable dtDaysInDocument = view.ToTable(true, "Agent");**//Used to distinct column value** 
     string[] dateRecord = dtDaysInDocument.Rows.Cast<DataRow>() 
          .Select(row => row["Agent"].ToString()) 
          .ToArray(); 



     DataTable dtFinal = new DataTable(); 
     dtFinal = dt.Clone(); 

     foreach (string str in dateRecord) 
     { 
      DataRow[] rows = dt.Select("Agent='" + str + "'"); 
      if (rows.Length > 1) 
      { 
       int finalValue = 0; 

       foreach (DataColumn col in dt.Columns) 
       { 
        if (col.ColumnName != "Agent") 
        { 
         foreach (DataRow row in rows) 
         { 
          int colValue = row[col] != null ? Convert.ToInt16(row[col]) : 0; 
          finalValue = colValue; 
          DataRow[] dtFinalrows = dtFinal.Select("Agent='" + str + "'"); 
          if (dtFinalrows.Length == 1) 
          { 
           string val = dtFinalrows[0][col.ColumnName].ToString(); 
           if (val != "") 
            finalValue = finalValue + Convert.ToInt16(val); 
           dtFinalrows[0][col.ColumnName] = finalValue; 
          } 
          else 
          { 
           DataRow finalDr = dtFinal.NewRow(); 
           finalDr[col.ColumnName] = finalValue; 
           finalDr["Agent"] = str; 
           dtFinal.Rows.Add(finalDr); 
          } 
         } 
        } 
       } 


      } 
      else if (rows.Length == 1) 
      { 
       DataRow finalDr = dtFinal.NewRow(); 
       foreach (DataColumn col in dt.Columns) 
       { 
        finalDr[col.ColumnName] = rows[0][col.ColumnName]; 
       } 
       dtFinal.Rows.Add(finalDr); 
      } 
+1

非常感謝你很好的算法:) –

+0

@RalphChristianDEclipse :) – Arshad

0

使用LINQ:

from t in dt.AsEnumerable() 
group t by t["Agent"] 
into newtable 
select new { 
Agent = newtable.Key; 
Product1 = newtable.Sum(c=>c["Product1"]); 
Product2 = newtable.Sum(c=>c["Product2"]); 
} 
+0

我該如何做得更簡單?因爲將來使用時用戶會添加一個產品,所以我怎樣才能讓它自動循環所有的列? 例如我添加product3列中的值,例如 –

0

試試這個

  DataTable dt1 = new DataTable(); 
      dt1.Columns.Add("Agent", typeof(string)); 
      dt1.Columns.Add("Product1", typeof(int)); 
      dt1.Columns.Add("Product2", typeof(int)); 
      dt1.Rows.Add(new object[] {"A", 1, 3}); 
      dt1.Rows.Add(new object[] {"B", 4, 5}); 
      dt1.Rows.Add(new object[] {"A", 5, 4}); 

      DataTable dt2 = dt1.Clone(); 

      var sum = dt1.AsEnumerable() 
       .GroupBy(x => x.Field<string>("Agent")) 
       .Select(m => new 
       { 
        key = m.Key, 
        p1 = m.Sum(s => s.Field<int>("Product1")), 
        p2 = m.Sum(s => s.Field<int>("Product2")) 
       }).ToList(); 

      foreach (var row in sum) 
      { 
       dt2.Rows.Add(new object[] { row.key, row.p1, row.p2 }); 
      } 

      dataGridView1.DataSource = dt2;