[英]how to refresh datagrid view from another form in c#
Firstly i wanna ask about how to refresh the Datagrid View from another form, because in this below codes i can't refresh it from the second form. 首先,我想问一下如何从另一种形式刷新Datagrid View,因为在下面的代码中,我无法从第二种形式刷新它。
This is how i refresh the datagrid view in "Cashier Transaction Form" . 这就是我在“收银机交易表单”中刷新datagrid视图的方式。
public void loadData()
{
var load = (from x in db.tbltransaction
join z in db.tblproduct
on x.product_ID equals z.ID
where x.status.Equals(0)
select new
{
TransactionID = x.belanjaID,
ProductID = x.produk_ID,
ProductName = z.namaBarang,
quantity = x.jumlahBarang,
Subtotal = x.subtotal
});
dgvtransaction.DataSource = load;
dgvtransaction.Columns[0].Visible = false;
}
nah. 罗。 what i want to do is when i add the data in 2nd form and i close it (so the event is --> FormClosed on "Cashier Search Product Form" ) can be load "dgvtransaction" in the "Cashier Transaction Form" . 我想做的是,当我以第二种形式添加数据并关闭它(因此事件为-> “ Cashier Search Product Form ”上的FormClosed)时,可以在“ Cashier Transaction Form”中加载“ dgvtransaction ” 。
This is the code on the 2nd form so far, to load data on the 1st dgv after closed). 到目前为止,这是第二个窗体上的代码,以便在关闭后在第一个dgv上加载数据。
Cashier Transaction Form pk;
private void Cashier Search Product Form_FormClosed(object sender, FormClosedEventArgs e)
{
pk.loadData();
}
The error is "Object reference not set to an instance of an object." 错误是“对象引用未设置为对象的实例”。
Thanks for any help. 谢谢你的帮助。
You're creating a new instance of the parent form, but what you need is to pass in a reference to the original. 您正在创建父表单的新实例,但是您需要传递对原始表单的引用。
public class CashierSearchProductForm
{
CashierTransactionForm pk;
public CashierSearchProductForm(CashierTransactionForm pk)
{
this.pk = pk;
}
private void CashierSearchProduct Form_FormClosed(object sender, FormClosedEventArgs e)
{
pk.loadData();
}
...
}
...
var csp = new CashierSearchProductForm(this);
Better yet, just subscribe to the FormClosed event from the original form: 更好的是,只需从原始表单订阅FormClosed事件:
var csp = new CashierSearchProductForm(this);
csp.FormClosed += (s,e) => pk.loadData();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.