Click here to Skip to main content
14,934,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two forms. Form 1 has a button[Display Data] which contains code to read a file and put the data into an array. Form 2 has a dataGridView. I want to load the array data from Form 1 into the dataGridView on Form 2. I am new to C# and can't seem to figure this out. I am using the latest version of Visual Studio updated about three weeks ago. What I have at this point is :

private void btn2Display_Click(object sender, EventArgs e)
     {

         string[] lines = File.ReadAllLines("C:\\c#_folder\\Ridge_Creek.txt");
         foreach (string line in lines)
         {
             string[] data = line.Split(',');
             for (int i = 0; i < data.Length; i++)
             {
                string date = data[i];
                double fee = Convert.ToDouble(data[i + 1]);
               double win = Convert.ToDouble(data[i + 2]);
                double totalfee = Convert.ToDouble(data[i + 3]);
               double totalwin = Convert.ToDouble(data[i + 4]);
               double netwin = Convert.ToDouble(data[i + 5]);
               i += 5;
               dataGridView2.Rows.Add(date, fee, win, totalfee, totalwin, netwin);
               dataGridView2.Sort(dataGridView2.Columns["Column1"], ListSortDirection.Ascending);
             }
         }
         dataGridView2.Visible = true;


     }


What I have tried:

I have tried reading answers to similar questions posted in Code Project and Stackoverflow but the answers to the questions were mostly over my head or not directly applicable to my problem. As far as I can determine the answer lies in the realm of delegates,events and eventhandlers. This is an area I am just starting to try to understand and don't have the skills to use for my issue.
Posted
Updated 17-Sep-20 8:45am

1 solution

Exactly how depends on the "relationship" between the two forms.
Have a look at these, one of them will fit your circumstances.
The form that creates an instance of another:
C#
MyForm mf = new MyForm();
mf.Show();
Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
   

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