两种不同形式的数据传输ı该怎么办? [英] two different form data transfer how can ı do?

查看:55
本文介绍了两种不同形式的数据传输ı该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种不同的形式.一种形式具有一个文本框和一个按钮,另一种形式具有datagrid.如果您为ex:12345输入一个id,然后单击按钮,则所有信息都来自数据库并填写表格.


我想这样做:如果ı搜索一个具有id的人,然后单击另一个打开的表单按钮,并且此人的信息填写了datagrid.

I have a two different form.one form has one textbox and one button and another form has datagrid. if you write a id for ex:12345 and click the button all information comes a database and form fill.


I want to do this:if ı search a person with id and click the button another form open and this person''s information fill in datagrid.

推荐答案

第二种形式的构造函数(上面带有datagrid的形式)添加变量.

然后使用此变量将数据加载到数据网格中.

然后,您可以通过在构造函数中传递"id"来将形式1传递给形式2.

伪代码(来自内存)
表格2构造函数
In the constructor of your second form (the one with the datagrid on it) add a variable.

Then use this variable to load the data into the datagrid.

You can then pass the ''id'' from form one to form two by passing it in the constructor.

Pseudo code (from memory)
form 2 constructor
public form2(int id)
    {
        InitializeComponent();
//load data here according to id variable
    }



调用form2



calling form2

form2 frm = new form2(12345);
       frm.show();


在您的第一个表单上,是一个按钮处理程序,用于从文本框&打开第二个表格

On your first form, a button handler to get the data from the text box & open the second form

private void buttonHandler_Activate(object sender, System.EventArgs e)
{
    // Assuming you've done some sort of validation
    int customerId = Convert.ToInt32(this.personTextBox.Text);

    // Assuming your second form is called 'Gridform'!
    using (GridForm grid = new GridForm ())
    {
        grid.GetCustomerDetails(customerId);

        // Doesn't have to be a dialog here
        grid.ShowDialog(this);
    }
}



在第二个窗体(GridForm)上,这是一个公共方法,该方法获取客户ID并填充屏幕.



On your second form (GridForm), a public method that takes the customer id and populates the screen.

public void GetCustomerDetails(int customerId)
{
   // Some sort of datbase operation here to get the data
   // This is copy-paste from microsoft 'DataSet' MSDN article

      string cString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
      SqlConnection myConnection = new SqlConnection(cString);
      // Create a SqlDataAdapter.
      SqlDataAdapter myAdapter = new SqlDataAdapter();
      myConnection.Open();
      SqlCommand myCommand = new SqlCommand("SELECT * FROM MyTable WHERE CustomerId = " + customerId.ToString(),
      myConnection);
      myCommand.CommandType = CommandType.Text;
   
      myAdapter.SelectCommand = myCommand;
      ds = new DataSet("Customers");
      myAdapter.Fill(ds);

// Now you've got data in a dataset, bind it to your grid on this form
    this.myGridReference.DataSource = ds;
    this.myGridReference.DataBind();
}



免责声明-我完全不支持躲避数据访问!但是你知道了

*在您的第一个表单上,验证数据输入,创建第二个表单的实例,将收集的数据传递给第二个表单

*第二种形式具有接收条目并自行填充的方法



Disclaimer - Dodgy data access that I don''t endorse at all! But you get the idea

* On you 1st form, validate data entry, create instance of second form, pass collected data to second form

* 2nd form has method to receive entry and populate itself


这篇关于两种不同形式的数据传输ı该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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