简体   繁体   中英

How to add to gridcontrol a new row at every click

I want to add new row to gridcontrol at every button click. 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

You can use the code below to add a new row in your 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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