我必须表格。
首先将BindingList<CustomObject>
作为DataSource
的数据网格视图。
第二个应该从第一个表单添加/删除/更新DataSource
。
我该怎么做?修改等在button_Click(object sender, EventArgs e)
上发生在secondform上。我可以将BindignList<>
ref
传递给SecondForms()
构造函数,但我无法将其进一步传递给button_Click()
答案 0 :(得分:1)
您可以在form2中创建一个form1将订阅的事件。把事情分开。 我不知道你是如何构建Form1和Form2所以我只是举个例子。
class Form2 : Something
{
public event NotifySubscriberEventHandler NotifySubscriberEvent ;
public void button_Click(object sender, EventArgs e)
{
var handler = NotifySubscriberEvent ;
if( handler != null)
{
handler(this,EventArgs.Empty) ;
}
}
}
class Form1
{
public BindingList<T> MyBindingList {get;set;} //
public void CreateForm2()
{
Form2 form2 = new Form2() ;
form2.NotifySubscriberEvent += OnButtonClicked;
}
public void OnButtonClicked(object source, EventArgs e)
{
//Do Something when notified
MyBindingList.Add(...)
}
}
您必须创建一个NotifySubsubscriberEventHandler委托。 这里: http://www.akadia.com/services/dotnet_delegates_and_events.html#Simple%20Event
但是你已经说过你将BindingList传递给我认为是这样的构造函数:
public class Form2
{
private BindingList<T> bindingList ;
public Form2(BindingList<T> bindingList)
{
this.bindingList = bindingList ;
}
public void button_Click(object sender, EventArgs e)
{
// Do bindingList.Add() or whatever
}
}
上述不起作用吗? ^^