簡體   English   中英

如何在每次單擊時向gridcontrol添加一個新行

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

我想在每次單擊按鈕時向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);
     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();
}

你得打電話

gridView1.DataBind();

設置DataSource后

您可以使用以下代碼在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);
}

您必須使用您的值更改列名稱和val1,val2,val3。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM