2017-09-04 6 views
0

2つのセルを結合して1つのセルにする方法はありますか(ビューではなくセル)。例えばDataTableの2つの同様の行を1つに結合する

前:

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

9  Pasta $20    1 

後:

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

以下私のコードは、それだけの量とaddAmntの値を変更し、上記に同じではありません。ステートメントが続行された後、DTには2つの行が1つではありません。

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部)と、感謝を:) –

関連する問題