2011-08-20 61 views
1

我有一個小問題DataGridView以一種形式更新gridview

我有兩種形式,一種形式的數據將由DataGridView填充。

當我們點擊gridview的列標題時,其他表單將打開相應的列值。

我已經做了在窗口2中的數據的一些操作,從而關閉窗口2之前,我想更新這些細節在Form1 gridview的...

我已經見過像在Form1事件處理程序附件的一些細節,但我沒有」找到確切的答案。

+0

爲什麼る給downvote .. – user903550

+0

任何一個請幫助我.... – user903550

+0

非常感謝您的支持。 ... – user903550

回答

1

表2碼

public event EventHandler<UpdatedEventArgs> updateEvent; 

    public class UpdatedEventArgs : EventArgs 
    { 
     public string SomeVal { get; set; } // create custom event arg for your need 
    } 

    protected virtual void OnFirstUpdateEvent(UpdatedEventArgs e) 
    { 
     if (updateEvent != null) 
      updateEvent(this, e); 
    } 


    private void button1_Click(object sender, EventArgs e) 
    { 
     UpdatedEventArgs eventData = new UpdatedEventArgs(); 
     eventData.SomeVal = "test"; // set update event arguments, according to your need 

     OnFirstUpdateEvent(eventData); 
    } 

    public Form2() 
    { 
     InitializeComponent(); 
    } 

表1碼

public Form1() 
    { 
     InitializeComponent(); 

     Form2 form2 = new Form2(); 
     form2.updateEvent += new EventHandler<Form2.UpdatedEventArgs>(form2_updateEvent); // create event handler to update form 1 from form 2 
     form2.Show(); 
    } 

    void form2_updateEvent(object sender, Form2.UpdatedEventArgs e) 
    { 
     if (e != null && e.SomeVal != null) 
     { 
      // Do the update on Form 1 
      // depend on your event arguments update the grid 
      //MessageBox.Show(e.SomeVal); 
     } 

    } 
+0

form1我需要添加任何東西..因爲你已經提到這兩個在form2 .... – user903550

+0

ohh thanq Dsw我會嘗試你的代碼.... – user903550

0

通過使用DataGrid,您最有可能像使用模型一樣使用DataTable。我會說,像DataTable參數傳遞給form2並操作form2中的SAME DataTable對象,所以對其行和提交進行的更改將自動反映在form1上。

+0

thanq .. tigran我現在就來試試吧... – user903550

+0

你可以看看這個問題http://stackoverflow.com/questions/7131040/how-to-up加載圖像從我們自己的系統和顯示圖像在一個p – user903550