我想在每次點擊按鈕時將新行添加到gridcontrol。我嘗試了很多方法,但沒有成功。我正在發送我的代碼。如何在每次點擊時向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();
}
首先你在我的第一個問題中使用我的代碼http://stackoverflow.com/questions/37187344/how-can-i-add-a-new-row-to-datagrid-at-every-button-click -in-winform –
在gridControl1.DataSource = dt之後添加gridControl1.DataBind() – Mainak
您必須更改gridControl1.DataSource = dt;到gridView1.DataSource = dt;試試吧 –