【问题标题】:How can I increment the row value by clicking a button如何通过单击按钮来增加行值
【发布时间】:2016-06-18 23:34:38
【问题描述】:

在我的按钮单击代码中,如下所示,如果行值“数量”具有相同的 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();

}

【问题讨论】:

    标签: c# devexpress-wpf


    【解决方案1】:

    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】:

      所以,在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

      【讨论】:

      • 我有 {"Specified cast is not valid."} 第二次点击同一项目时出错。
      • 您是否将列类型指定为string 而我将其指定为int 检查列类型。哦,我说的是QuantityProductId 专栏
      【解决方案3】:

      我解决了我的问题并且效果很好。我有 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;
      
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-28
        • 2014-12-09
        • 2021-05-06
        • 2021-07-23
        相关资源
        最近更新 更多