在我点击按钮的代码中,如下所示,我想将行值“Quantity”增加1,如果它具有相同的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();
}
答案 0 :(得分: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);
}
}
工作示例here
答案 1 :(得分:0)
Here是如何在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);
}
答案 2 :(得分:0)
我解决了我的问题并且效果很好。我有2个网格,第一个数据传输来自另一个表单网格数据源,第二个数据源是通过捕获第一个数据来生成的。 当我点击按钮&gt;&gt;来自第一网格的数据减1,第2网格相应地增加1。 这是代码,我将尝试改进它:
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;
}