将DataTable中的两个相似行合并为一个

时间:2017-09-04 09:10:28

标签: c# datatable

有没有办法将两个单元格组合成一个单元格(不是视图,而是单元格)? e.g。

在:

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

9     Pasta    $20             1

后:

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

我的代码如下,在上面不一样,它只更改quantity和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);
}

1 个答案:

答案 0 :(得分:0)

如果我理解你想要的东西,我想你想做这样的事情:

Run > Edit Configurations