Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way I've done this in the past is to pass a Action delegate to the second form that references a method from the first form.</p> <p>The method you pass in contains the logic that updates your DataGridView.</p> <p>Then in your second form closing event you call this delegate (after checking that it is not null) passing the value from your textbox.</p> <p>Below is some quick prototype code to show how to do this. My method from Form1 just shows a message box, but you can easily change this to update your DataGridView datasource.</p> <pre><code>public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 form = new Form2(); Action&lt;string&gt; showMessage = ShowMessage; form.ClosingMethod(showMessage); form.Show(); } private void ShowMessage(string message) { MessageBox.Show(message); } } public partial class Form2 : Form { private Action&lt;string&gt; _showMessage; public Form2() { InitializeComponent(); } public void ClosingMethod(Action&lt;string&gt; showMessage) { _showMessage = showMessage; } private void Form2_FormClosing(object sender, FormClosingEventArgs e) { if (_showMessage != null) { _showMessage("hippo"); } } } </code></pre> <p><strong>Edit</strong></p> <p>Just occurred to me that the call to the delegate <code>_showMessage("hippo");</code> is blocking.</p> <p>Your form will not close until the delegate completes - potentially a long time. In my message box example, the form doesn't close until the OK button is clicked.</p> <p>To get around this you can call your delegate asynchronously as shown below:</p> <pre><code>private void Form2_FormClosing(object sender, FormClosingEventArgs e) { if (_showMessage != null) { _showMessage.BeginInvoke("hippo", null, null); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload