如何在winform中的每个按钮点击添加一个新行到datagrid [英] how can I add a new row to datagrid at every button click in winform
本文介绍了如何在winform中的每个按钮点击添加一个新行到datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在每次按钮点击时向网格控件添加新行。
我尝试了很多方法,但没有成功。我正在发送我的代码
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);
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();
}
有没有人帮助我上面的?提前感谢您的宝贵答复。
Is there anyone to help me out for the above? Thanks in advance for your precious replies.
推荐答案
尝试这样:
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();
这篇关于如何在winform中的每个按钮点击添加一个新行到datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文