The way I did this in the past is to pass the Action delegate to the second form, which refers to the method from the first form.
The method you pass contains logic that updates your DataGridView.
Then in the second closing event, you call this delegate (after checking that it is not null), passing the value from your text field.
Below is a quick prototype code to show how to do this. My method from Form1 just shows the message box, but you can easily change it to update the DataGridView data source.
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 form = new Form2(); Action<string> showMessage = ShowMessage; form.ClosingMethod(showMessage); form.Show(); } private void ShowMessage(string message) { MessageBox.Show(message); } } public partial class Form2 : Form { private Action<string> _showMessage; public Form2() { InitializeComponent(); } public void ClosingMethod(Action<string> showMessage) { _showMessage = showMessage; } private void Form2_FormClosing(object sender, FormClosingEventArgs e) { if (_showMessage != null) { _showMessage("hippo"); } } }
Edit
It just occurred to me that the delegate call _showMessage("hippo"); is blocked.
Your form will not be closed until the delegation is complete - possibly for a long time. In my example message box, the form does not close until the OK button is clicked.
To get around this, you can call your delegate asynchronously, as shown below:
private void Form2_FormClosing(object sender, FormClosingEventArgs e) { if (_showMessage != null) { _showMessage.BeginInvoke("hippo", null, null); } }
source share