0

在我的按钮单击代码中,如下所示,如果行值“数量”具有相同的 id,我想将其增加一,如果没有,则开始一个新行。我怎样才能做到这一点?

private void BProduct_Click(object sender, EventArgs e)
{
    spGridRowClick.Visibility = Visibility.Hidden;
    decimal Quantity;
    decimal.TryParse(txtKeyPad.Text, out Quantity);
    if (Quantity <= 1) Quantity = 1;       

    Button bt = (Button)sender;

    productId =(int)bt.Tag;

    BOneRestEntities db = new BOneRestEntities();

    var results = from inv in db.Inventory
                  where inv.RecId == productId
                  select new
                  {
                      ProductId = inv.RecId,
                      inventoryName = inv.InventoryName,
                      Quantity,
                      Total = Quantity * inv.InventoryPrice
                  };

    foreach (var x in results)
    {
        DataRow newRow = dt.NewRow();


        newRow.SetField("inventoryName", x.inventoryName);
        newRow.SetField("Quantity", x.Quantity);
        newRow.SetField("Total", x.Total);
        newRow.SetField("ProductId", x.ProductId);
        dt.Rows.Add(newRow);

    }
    txtKeyPad.Clear();
    gridCalculate.ItemsSource = dt;
    gridCalculate.View.MoveNextRow();
    TotalRow();

}
4

3 回答 3

0

因此,在DataRow newRow = dt.NewRow();您检查该行是否已经存在之前。

如果确实存在,则修改并更改数量。

如果它不存在,像你已经做的那样创建一个新的。

像这样的东西:

foreach (var item in results)
{
    var row = dt.AsEnumerable().FirstOrDefault(x => (int)x["ProductId"] == item.Id);
    int quantity = item.Quantity;

    if (row == null)
    {
        row = dt.NewRow();
        row.SetField("Quantity", quantity);
        row.SetField("ProductId", item.Id);
        dt.Rows.Add(row);
    }
    else
    {
        quantity += (int)row["Quantity"];
        row.SetField("Quantity", quantity);
    }
}

这里的工作示例

于 2016-06-16T16:00:22.963 回答
0

我解决了我的问题并且效果很好。我有 2 个网格,第一个的数据源来自另一个表单网格数据源,第二个的数据源是通过捕获第一个的 id 生成的。当我单击按钮>>时,来自第一个网格的数据减一,第二个网格的数据相应地增加一。这是代码,我将尝试改进它:

private void button_PreviewMouseDown(object sender, MouseButtonEventArgs e)

{  
if (gridSplitPayment.View.IsRowSelected(gridSplitPayment.View.FocusedRowHandle))

{
  decimal a, b;
  int  productId = Convert.ToInt32(gridSplitPayment.GetCellValue(gridSplitPayment.View.FocusedRowHandle, "ProductId").ToString());
a = Convert.ToDecimal(gridSplitPayment.GetCellValue(gridSplitPayment.View.FocusedRowHandle, "Quantity"));
b = a - 1;

gridSplitPayment.SetCellValue(gridSplitPayment.View.FocusedRowHandle, "Quantity", b);


                BOneRestEntities db = new BOneRestEntities();
                var results = from inv in db.Inventory
                              where inv.RecId == productId
                              select new
                              {
                                  inventoryName = inv.InventoryName,
                                  Quantity = inv.Quantity,
                                  Total = b * inv.InventoryPrice,
                                  ProductId = inv.RecId
                              };

                foreach (var x in results)
                {                    gridSplitPayment.SetCellValue(gridSplitPayment.View.FocusedRowHandle, "Total", x.Total);
                }
gridSplitPayment.RefreshRow(gridSplitPayment.View.FocusedRowHandle);
                gridSplitPayment.RefreshData();
}
int proId = Convert.ToInt32(gridSplitPayment.GetCellValue(gridSplitPayment.View.FocusedRowHandle, "ProductId").ToString());
decimal qty = Convert.ToDecimal(gridSplittedPaymentRight.GetCellValue(gridSplittedPaymentRight.View.FocusedRowHandle, "Quantity"));
            BOneRestEntities dbe = new BOneRestEntities();

            var result = from inv in dbe.Inventory
                         where inv.RecId == proId
                         select new
                         {
                             ProductId = inv.RecId,
                             inventoryName = inv.InventoryName,
                             Quantity = qty,
                             Total =  inv.InventoryPrice
                         };


            foreach (var y in result)
            {
                if (qty == 0)
                {
                    DataRow row = dt1.NewRow();
                row.SetField("inventoryName", y.inventoryName);
                    row.SetField("Quantity", y.Quantity + 1);
                    row.SetField("Total", y.Total);
                row.SetField("ProductId", y.ProductId);

                    dt1.Rows.Add(row);
                }


                else
                {
                    gridSplittedPaymentRight.SetCellValue(viewSplittedPayment.FocusedRowHandle, "Quantity", qty + 1);
                    gridSplittedPaymentRight.SetCellValue(viewSplittedPayment.FocusedRowHandle, "Total", qty*y.Total);
                }

            }

            gridSplittedPaymentRight.ItemsSource = dt1;

            }
于 2016-06-18T15:34:38.200 回答
0

以下是如何在 DataTable 中搜索包含具有特定值或主键的列的行,这似乎是ProductId

所以在你的情况下,你会做这样的事情:

    string qry = string.Format("ProductId == '{0}'", productid);
    DataRow existing = dt.Select(qry).FirstOrDefault();

    if (existing != null) 
    {
        // increment value
    } 
    else
    {
        DataRow newRow = dt.NewRow();
        //... fill in new row
        dt.Rows.Add(newRow);
    }
于 2016-06-16T16:17:28.103 回答