在Windows应用程序中将多个数据保存在变量中并从varibale中检索数据 [英] Save multiple data in variable and retrive data from varibale in windows application

查看:80
本文介绍了在Windows应用程序中将多个数据保存在变量中并从varibale中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式,一种用于用户输入,第二种用于显示输出.用户可以输入ID,名称和5名雇员的输入形式的文本框中的地址.在该用户转到outputforms之后,他可以通过输入employee ID来查看特定员工的记录.那么如何在变量中存储雇员的多个数据并检索雇员的数据呢?

解决方案


将您的详细信息放在数据表中,然后将其传递给第二种形式.为此,您需要使用全局类文件来获取和设置DataTable的值.
Class1.cs

  class  Class1
{
    静态 DataTable dt =  DataTable();
    公共 静态 DataTable GetData()
    {
        返回 dt;
    }

    公共 静态  void  SetData(DataTable dt1)
    {
        dt = dt1;
    }
} 


Form1:

 私有 无效 button1_Click(对象发​​件人,EventArgs e)
{
    DataTable dt =  DataTable();
    DataColumn dc =  DataColumn(" );
    dt.Columns.Add(dc);
    dt.Rows.Add(" );
    dt.Rows.Add(" );
    dt.Rows.Add(" );
    Class1.SetData(dt);
}
私有 无效 button2_Click(对象发​​件人,EventArgs e)
{
    Form2 f =  Form2();
    f.Show();
} 


Form2:

 私有  void  Form2_Load(对象发​​件人,EventArgs e)
{
    DataTable dt = Class1.GetData();
    dataGridView1.DataSource = dt;
} 





--Amit


请参阅.解决方案3将是您的理想之选. 在应用程序启动期间获取变量值 [ ^ ]

有很多方法可以实现.
1.创建一个类(对象,例如Emp)以存储所需的所有数据,并修改第二种表单构造方法以包括该类对象; List< emp>因为您有5名员工.还要创建私人成员来保存此内容.

  public  Form2(列表< emp> empList)
        {
             this  .EmpList = empList;
            InitializeComponent();
        } 


当您准备好调用Form2时,请在列表中加入"construction"参数
2.您将所有List< emp>在Form1上作为公共属性,为:

 私有列表< emp> _empList;
        公共列表< emp> EmpList
        {
            获取 {返回 _empList; }
             set  {_empList =  value ; }
        } 



准备将Form2视为:
时,将第二个窗体的父级作为Form1.

 使用(Form2 f2 =  Form2())
            {
                f2.Owner = ;
                f2.ShowDialog();
            } 


并且可以在Form2上设置为(注意:这是Form2的完整代码,仅用于演示):

 公共 部分  class  Form2
    {
       Form1 FormOwner;
       私有 布尔 [] check =   bool  [ 100 ];
       私有列表< emp> EmpList;
       公共 Form2
        {
            InitializeComponent();
        }
        
        私有 无效 Form2_Load(对象发​​件人,EventArgs e)
        {

             .FormOwner =(Form1) .Owner;
             .EmpList = FormOwner.EmpList;
      
        } 


I have two forms one for user input and second for display output. user can input id, name & address in textbox of input forms of 5 employee. after that user goes to outputforms and he can view record of specific employee by enter the id of employee. so how can store multiple data of employee in varibale and retrive data of employee?

解决方案

Hi,
Sore your details in the datatable and pass that to the second form. For this you need to use a global class file to get and set the values of a DataTable.
Class1.cs

class Class1
{
    static DataTable dt=new DataTable();
    public static DataTable GetData()
    {
        return dt;
    }

    public static void SetData(DataTable dt1)
    {
        dt = dt1;
    }
}


Form1:

private void button1_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    DataColumn dc = new DataColumn("values");
    dt.Columns.Add(dc);
    dt.Rows.Add("A");
    dt.Rows.Add("B");
    dt.Rows.Add("C");
    Class1.SetData(dt);
}
private void button2_Click(object sender, EventArgs e)
{
    Form2 f = new Form2();
    f.Show();
}


Form2:

private void Form2_Load(object sender, EventArgs e)
{
    DataTable dt = Class1.GetData();
    dataGridView1.DataSource = dt;
}





--Amit


See it..Solution 3 would be one for you..
Retrieving a variable value during application startup[^]


There are many ways to this.
1. Create a class(object, say Emp) to store all the data you need and modify your second form construction method to include the class object; possibly a List<emp> as you have 5 employees. Also create private members to hold this.

public Form2(List<emp> empList)
        {
            this.EmpList = empList;
            InitializeComponent();
        }


And when you are ready to call Form2, include the argument to construction as your list
2.Here you store all your List<emp> on Form1 as Public property as:

private List<emp> _empList;
        public List<emp> EmpList
        {
            get { return _empList; }
            set { _empList = value; }
        }



Make the second form''s parent as Form1 when you ready to view Form2 as :

using (Form2 f2 = new Form2())
            {
                f2.Owner = this;
                f2.ShowDialog();
            }


and on Form2 can be set as(note: this is the entire code for Form2,just to demonstrate):

public partial class Form2 
    {
       Form1 FormOwner;
       private bool[] check = new bool[100];
       private List<emp> EmpList;
       public Form2
        {
            InitializeComponent();
        }
        
        private void Form2_Load(object sender, EventArgs e)
        {

            this.FormOwner = (Form1)this.Owner;
            this.EmpList = FormOwner.EmpList;
      
        }


这篇关于在Windows应用程序中将多个数据保存在变量中并从varibale中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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