Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?
Posted
Comments
ridoy 17-Aug-12 2:13am    
You need to use database..Do you have any concept about that or do you like to use it?You can also use files instead of database.It's up to you what you will prefer?
Manohar Khillare 17-Aug-12 2:15am    
i dont want to use databse . i wnat it should be done using memory variable

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
C#
class Class1
{
    static DataTable dt=new DataTable();
    public static DataTable GetData()
    {
        return dt;
    }

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

Form1:
C#
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:
C#
private void Form2_Load(object sender, EventArgs e)
{
    DataTable dt = Class1.GetData();
    dataGridView1.DataSource = dt;
}




--Amit
 
Share this answer
 
Comments
ridoy 17-Aug-12 2:30am    
This one is better one.
_Amy 17-Aug-12 2:31am    
Thank you ridoy. :)
Manohar Khillare 17-Aug-12 2:34am    
see i dont want should be use datatable i want different method like multidimensional array?
_Amy 17-Aug-12 2:37am    
Then use the same functions. Not a trouble. Instead of datatable pass array or something like list, hashtables according to your wish. Try it. :)
ridoy 17-Aug-12 2:40am    
Yes you can use array instead of datatable but process will be same..
See it..Solution 3 would be one for you..
Retrieving a variable value during application startup[^]
 
Share this answer
 
Comments
Manohar Khillare 17-Aug-12 2:20am    
can it should be done using class file?
ridoy 17-Aug-12 2:31am    
you can
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.

C#
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:

C#
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 :
C#
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):
C#
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;
      
        }
 
Share this answer
 
v3

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