1

I have 2 forms. Form1 contains a datatable. When I instantiate form2, I pass it one record from form1's datatable. Then I do some things in form2, and now I want to update the datatable in Form1, based on what was done in form2.

How can I accomplish this? I need to remove datarows, and add new datarows, and I must have the data from form2 to accomplish this.

MAW74656
  • 3,449
  • 21
  • 71
  • 118
  • [This one](http://stackoverflow.com/questions/280579/c-sharp-beginner-help-how-do-i-pass-a-value-from-a-child-back-to-the-parent-for) for example. I did [this search](http://stackoverflow.com/search?q=pass+value+back+form) on SO. – Otiel Nov 17 '11 at 22:48
  • @Otiel -Ok, I get that part. Here's where I'm stuck: I'm done manipulating the data on form2, how do I tell form1 to come pick up the data before I close form2? – MAW74656 Nov 17 '11 at 22:52
  • Do you use `form2.Show()` or `form2.ShowDialog()`? Isn't the `string result = formOptions.GetMyResult;` line from [this answer](http://stackoverflow.com/questions/280579/c-sharp-beginner-help-how-do-i-pass-a-value-from-a-child-back-to-the-parent-for/280586#280586) satisfying for you? – Otiel Nov 17 '11 at 22:53
  • @Otiel -I use Show(), how would ShowDialog() help? I still don't understand. – MAW74656 Nov 17 '11 at 22:54
  • By using `ShowDialog()`, you'll make your `form2` modal and the run will be waiting on the `form2.ShowDialog()` line to continue. The solution is then simply to add `string result = formOptions.GetMyResult;` just after the `form2.ShowDialog()` line. – Otiel Nov 17 '11 at 22:57
  • @Otiel -If I can send any datatype through that way, then perfect. YOu should post your last comment as an answer. – MAW74656 Nov 17 '11 at 22:58
  • I'd rather think you should delete your question if you got the solution now. It is likely a mod will close it as exact duplicate. – Otiel Nov 17 '11 at 23:00

1 Answers1

1

From this post:

  • Create a public property in form2:

    public partial class Form2 : Form {
    
        private DataTable data;
    
        public DataTable Data {
            get { return data; }
        }
    }
    
  • And access it in form1 when you're done with form2:

    //...
    form2.ShowDialog();
    // Retrieved modified dataTable
    dataTable = form2.Data;
    //...
    
Community
  • 1
  • 1
Otiel
  • 18,404
  • 16
  • 78
  • 126