Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all..How to pass a dataobject that is created in one form into another form so that form2 can carry out another method?Thanks..
Posted
Comments
Sergey Alexandrovich Kryukov 25-Jul-12 3:44am    
What form? System.Windows.Forms.Form? A Web form? ASP.NET or not? Depending on that, the answers would be very different...
--SA
Esmond90 25-Jul-12 3:55am    
Yes,it is System.Windows.Forms.Form..Thanks for the help ya.=)

Since your question is not very clear, I assume that your data object is a data table, and You have two win forms FORM1 and FORM2.

As I understand you have DataTable object (dtForm1) built in FORM1 and you want dtForm1 to be available for FORM2.

Create A static Class called DataTransHelper like below:

public static class DataTransHelper
{
public static DataTable dt {get; set;}
}

In Form1 as soon as you build object dtForm1, you set this to "dt" of DataTransHelper like below.

//dtable = YourDataAcces.GetData();
DataTransHelper.dt = dtForm1;

Since DataTransHelper class is static, it is not instance specific, hence members of DataTransHelper are same to both FORM1 and FORM2.

So in Form2, you can access DataTransHelper.dt and use it as you need it like below.
DataTable dtForm2 = DataTransHelper.dt;
 
Share this answer
 
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

—SA
 
Share this answer
 
<small><big><pre lang="Javascript">
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900