如何在Window应用程序中的C#中将数据从一种形式传输到另一种形式. [英] How to transfer data from one form to another form in c# in window application.

查看:127
本文介绍了如何在Window应用程序中的C#中将数据从一种形式传输到另一种形式.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview表单,其名称为"Form1".datagridview的列名称为"id".
现在,我想将列名"id"传输(检索)到窗口应用程序中的"Form2"中.
那该怎么办,因为我们无法在窗口应用程序中使用会话".
因此,给出答案.

I have a datagridview a form which have name "Form1".The datagridview have the column name "id".
Now I want to transfer(retrieve) the column name "id" into the "Form2" in window apps.
So what can I do because we cannot use the "Session" in window apps.
So give the answer.

推荐答案

这取决于您的工作.
听起来,您正在打开一个Form2的新实例,专门显示与所选ID相关的数据.
如果是这样,那么最好的方法是更改​​Form2的构造函数以接受一个id值,并在构造它时对其进行处理.

还有其他方法,但正如我所说-这取决于您在做什么.
It depends on what you are doing.
By the sound of it, you are opening a new instance of Form2 specifically to show data related to to the selected id.
If so, then the best way is to change the constructor of Form2 to accept an id value, and process it as it is constructed.

There are other ways, but as I say - it depends on what you are doing.


这是
this is the article will completely explaining you various ways available for pass data between forms.
as i was facing same issue when i was newbie to Visual C#.net and that article helps me lot.
i hope it will helps to you also.
Thanks.....


ASP.NET(Web应用程序)中的数据传输方法比C#.NET(基于Window的应用程序)中的数据传输方法容易得多. ASP.NET使用自己的stateManagement技术

与C#.NET中的VB.NET不同,我们不能直接使用其所有控件和值访问所有形式.

可以使用以下方法完成

1.清除formTwo上的公共验证,请参见下面的代码
Data transfer methods are much easier in ASP.NET (web application) than in C#.NET (Window Based application).
ASP.NET uses their own stateManagement technique

Unlike VB.NET in C#.NET we can not access all forms with it''s all controls and values directly.

it can be done using following ways

1. Declear a public veriable at formTwo, see the below code
//FormTwo
public string szUserId;
onForm_Load()
{
  Label.Text = szUserId;
}


现在在单击FormOne时显示FormTwo
通过创建FormTwo的对象,我们可以访问全局veriable并为其分配值


now show FormTwo on click on FormOne
By creating object of FormTwo, we can Access global veriable and assign values to it

FormOne_Button1_Click()
{   
//create a object of formTwo
   FormTwo frmtwo = new FormTwo();
//Access global veriable and assign values to it
  frmtwo.szUserId = txtUserId.Text;
//Hide formOne
  this.Hide();
//show formtwo
  frmTwo.Show();
}



2.在formTwo上的构造函数中清除值



2. Declear value in constructor on formTwo

//FormTwopublic 
string szUserId;
//in formtwo Constructor assign a value to it's veriable

FormTwo (string Uid)
{     
  Init()
  this.szUserId = Uid
}

onForm_Load()
{
  Label.Text = szUserId;
}




在FormOne上创建FormTwo的对象时传递userid值




on FormOne while creating Object of FormTwo pass the userid value

//FormOne FormTwo 
frmtwo = new FormTwo(txtUserId.Text);
frmTwo.Show()


这篇关于如何在Window应用程序中的C#中将数据从一种形式传输到另一种形式.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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