如何将以一种形式创建的数据对象传递给另一种形式? [英] How to pass dataobject that is created in one form into another form?

查看:82
本文介绍了如何将以一种形式创建的数据对象传递给另一种形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..如何将以一种形式创建的数据对象传递给另一种形式,以便form2可以执行另一种方法?由于您的问题不是很清楚,所以我假设您的数据对象是一个数据表,并且您有两个获胜表格FORM1和FORM2.

据我了解,您已经在FORM1中构建了DataTable对象(dtForm1),并且您希望dtForm1可用于FORM2.

创建一个名为DataTransHelper的静态类,如下所示:

公共静态类DataTransHelper
{
公共静态数据表 dt {get;设置;}
}

在构建对象dtForm1后,立即在Form1中将其设置为DataTransHelper的"dt" ,如下所示.

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

由于DataTransHelper类是静态的,因此它不是特定于实例的,因此DataTransHelper的成员与FORM1和FORM2相同.

因此,在Form2中,您可以访问DataTransHelper.dt并根据需要使用它,如下所示.
DataTable dtForm2 = DataTransHelper.dt;


这是有关表单协作的常见问题.最健壮的解决方案是在Form类中实现适当的接口,并传递接口引用而不是对Form的整个实例"的引用.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [</big></small></pre>


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..

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;


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


<pre><small><big><pre lang="Javascript"></pre></big></small></pre>


这篇关于如何将以一种形式创建的数据对象传递给另一种形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆