2012-09-16 25 views
0

我必須形成。將BindingList <CustomObject>傳遞給第二種形式的button_Click方法

首先有BindingList<CustomObject>作爲DataSource的datagridview。

第二個應該從第一個表格中添加/刪除/更新DataSource

我該怎麼做?第二種形式在button_Click(object sender, EventArgs e)中發生修改等。我可以ref傳遞BindignList<>SecondForms()構造,但我不能進一步將它傳遞給button_Click()

+0

單獨創建表單還是創建form1創建表單2 –

+0

@LewsTherin是,SecondForm正在FirstForm中創建 – Saint

+0

您是否嘗試在form1中創建Form1將訂閱的事件? –

回答

1

你可以做的是建立在窗口2注意Form 1將訂閱的事件。保持事物的分離。 我不知道你是如何構建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 

    } 
} 

難道上面不行? ^^

+0

但是我怎樣才能在Form1中獲得在Form2中更改的BindingList ? – Saint

+0

您不必在Form2中更改它。 如果BindingList 是Form1的成員,則您已經可以在OnButtonClicked方法中訪問它..我將更新我的答案以反映我的意思 –

+0

@Saint看看我的第一個建議。 –

相關問題