2017-09-04 75 views
0

是否有任何方法將兩個單元合併爲一個(不是視圖,而是單元)?例如將DataTable中的兩個相似行合併爲一個

前:

ID | Item | Unit Price | Qty 
------------------------------- 
8  Pasta $20    1 

9  Pasta $20    1 

後:

ID | Item | Unit Price | Qty 
------------------------------- 
8  Pasta $20    2 

我下面的代碼,是不是上面的一樣,它只是改變數量和addAmnt的價值。在聲明繼續之後,DT應該有兩行而不是一行。

if (TransactionControl.SelectedTable == TransactionTypes.Edit) 
{ 
    bool[] bb = new bool[] 
    { 
     true, //while 
     true, //Row Count = 0 & 1 
     false //Merge 
    }; 

    Int32 origProductID = Convert.ToInt32(TransDetailDT.CurrentRow.Cells[2].Value); 
    decimal origQty = Convert.ToDecimal(TransDetailDT.CurrentRow.Cells[6].Value); 
    decimal origSRP = Convert.ToDecimal(TransDetailDT.CurrentRow.Cells[7].Value); 
    decimal addAmnt = TransactionControl.AddAmt; 

    DataTable DT; 
    do 
    { 
     DT = tblTransactionDetailTA.GetTDetail(TransactionControl.TransactionID, Convert.ToInt32(TransDetailDT.CurrentRow.Cells[2].Value), addAmnt); 
     if(bb[1] && DT.Rows.Count == 0) 
     { 
      tblTransactionDetailTA.UpdateSingleItem(Convert.ToDecimal(TransactionControl.Qty), addAmnt, origQty, origProductID, origSRP, TransactionControl.TransactionID); 
      bb[1] = false; 
      bb[2] = true; 
      continue; 
     } 
     if(bb[1] && DT.Rows.Count == 1) 
     { 
      DataRow DR = DT.Rows[0]; 
      DR["Quantity"] = numQty.Value; 
      DR["SpecialPrice"] = addAmnt; 

      DR.EndEdit(); 
      tblTransactionDetailTA.Update(DR); 

      bb[1] = false; 
      bb[2] = true; 
      continue; 
     } 
     if (bb[2] && DT.Rows.Count > 1) 
     { 
      DT.Rows[0]["Quantity"] = Convert.ToDouble(DR1["Quantity"]) + 
      Convert.ToDouble(DR2["Quantity"]); 
      DT.Rows[1].Delete(); 
      tblTransactionDetailTA.Update(DT); 
      bb[0] = false; 
     } 
     else { bb[0] = false; MessageBox.Show("false"); } 
    } 
    while (bb[0]); 
    qryTransactionDetailTA.Fill(litePOSDataSet.qryTransactionDetail); 
} 

回答

0

如果我知道你想好什麼,我覺得你想要做這樣的事情:

// Below is for my sample data 
var dt = new DataTable(); 
var idCol = dt.Columns.Add("ID", typeof(int)); 
var itemCol = dt.Columns.Add("Item", typeof(string)); 
var upCol = dt.Columns.Add("Unit Price", typeof(int)); 
var qtyCol = dt.Columns.Add("Qty",typeof(int)); 
dt.Rows.Add(8, "Pasta", 20, 1); 
dt.Rows.Add(9, "Pasta", 20, 1); 

// I generate my expected result by using linq 
var mergedData = dt.Rows.OfType<DataRow>() 
    .GroupBy(g=> g.ItemArray[itemCol.Ordinal]) 
    .Select(c=> new 
    { 
     item = c.Key, 
     id = c.FirstOrDefault()?[idCol],     // I think you need first id as result 
     up = c.Average(x=> int.Parse(x[upCol].ToString())), // I think you need average of Unit Prices as result 
     qty = c.Sum(x => int.Parse(x[qtyCol].ToString())) // I think you need sum of Quantity as result 
    }); 

// A clone of current DataTable will give me its structure without data 
// I use this as a temporary DataTable 
var newDt = dt.Clone(); 
foreach (var md in mergedData) 
{ 
    newDt.Rows.Add(md.id, md.item, md.up, md.qty); 
} 

dt = newDt; 
+0

最後一部分作品(newDT部分),並感謝:) –