简体   繁体   English

如何在每次单击时向gridcontrol添加一个新行

[英]How to add to gridcontrol a new row at every click

I want to add new row to gridcontrol at every button click. 我想在每次单击按钮时向gridcontrol添加新行。 I tried many ways but no success. 我尝试了很多方法但没有成功。 I am sending my code. 我正在发送我的代码。

 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);
     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();
}

You got to call 你得打电话

gridView1.DataBind();

after you set the DataSource 设置DataSource后

You can use the code below to add a new row in your GridControl : 您可以使用以下代码在GridControl中添加新行:

gridView1.AddNewRow();

int rowHandle = gridView1.GetRowHandle(gridView1.DataRowCount);
if (gridView1.IsNewItemRow(rowHandle))
{
    gridView1.SetRowCellValue(rowHandle, gridView1.Columns["ColumnName1"], val1);
    gridView1.SetRowCellValue(rowHandle, gridView1.Columns["ColumnName2"], val2);
    gridView1.SetRowCellValue(rowHandle, gridView1.Columns["ColumnName3"], val3);
}

You must change columns name and val1, val2, val3 with your values. 您必须使用您的值更改列名称和val1,val2,val3。

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

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