繁体   English   中英

我如何在winform中的每个按钮单击时向datagrid添加新行

[英]how can I add a new row to datagrid at every button click in winform

我想在每次单击按钮时向gridcontrol添加新行。 我尝试了很多方法但没有成功。 我正在发送我的代码。

private void B_Click(object sender, EventArgs e)
{
   Button bt = (Button)sender;
   int productId = (int)bt.Tag;
   AddProductDataContext db = new AddProductDataContext();
   decimal Quantity;
   decimal.TryParse(txtCalculator.Text, out Quantity);
   gridControl1.DataSource = from inv in db.Inventories where inv.RecId == productId
      select new
      {
         inventoryName = inv.InventoryName,
         Quantity,
         Total = Quantity * inv.InventoryPrice
      };
   gridView1.AddNewRow();
   gridView1.UpdateCurrentRow();
} 

有没有人可以帮我解决上述问题? 预先感谢您的宝贵答复。

尝试这个 :

Button bt = (Button)sender;
       int productId = (int)bt.Tag;
       AddProductDataContext db = new AddProductDataContext();
       decimal Quantity;
       decimal.TryParse(txtCalculator.Text, out Quantity);
     var results  = from inv in db.Inventories
                                          where inv.RecId == productId
                                          select new
                                          {
                                              inventoryName = inv.InventoryName,
                                              Quantity,
                                              Total = Quantity * inv.InventoryPrice
                                          };

               DataTable dt = new DataTable();
               dt.Columns.Add("inventoryName");
               dt.Columns.Add("Quantity");
               dt.Columns.Add("Total");

               foreach (var x in results)
               {
                   DataRow newRow = dt.Rows.Add();
                   newRow.SetField("inventoryName", x.inventoryName);
                   newRow.SetField("Quantity", x.Quantity);

                   newRow.SetField("Total", x.Total);

               }

               gridControl1.DataSource = dt;
               gridView1.AddNewRow();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM