Click here to Skip to main content
16,016,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am currently writing a program which needs an edit form. This form is to edit a country in the datagridview which I have. I am however confused about how to pass the data from the data gridview in the main form to the relative places on the edit form. I have done most of the coding as shown below. I know I need a method in my AVL class to return all the information of the selected country back to the main form method which then passes it back to the edit form and displays it. If anyone can help me to complete this method I would be grateful, I am unsure what to pass and what to write in this method.

Main form method

C#
public void button2_Click(object sender, EventArgs e)
      {
          // creates new form referencing data grid view and AVL Tree
          EditCountryForm editform = new EditCountryForm(ref this.countryTree, dataGridView1);

          string CountryName = "";

          //set name of country selected to string
          foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
          {
              CountryName = item.Cells[0].Value.ToString();

          }

          // create new country with that name
          Country FindRow = new Country(CountryName, 0, 0, 0, 0,null);
          // create new country to be passed to edit form
          Country EditCountry = countryTree.ReturnCountryInfo(FindRow);

          editform.passCountry(EditCountry);

          editform.Show(); // Shows Form


Edit Form Method

C#
public void passCountry(Country SelectedCountry)
       {
           this.ToEdit = SelectedCountry;

           CountryNameText.Text = ToEdit.name;
           CountryText.Text = ToEdit.Name;
           GDPGrowthText.Text = ToEdit.GdpGrowth.ToString();
           InflationText.Text = ToEdit.Inflation.ToString();
           HDIRankingText.Text = ToEdit.HdiRankings.ToString();
           TradeBalanceText.Text = ToEdit.TradeBalance.ToString();


       }


AVL Tree method

C#
public Country ReturnCountryInfo(Country FindRow)
        {
            // Needs to return all information about the selected country 

            Country SelectedCountry = new Country("", 0, 0, 0, 0,null);
            return SelectedCountry;
        }
Posted

Have a look at this: Transferring information between two forms, Part 1: Parent to Child[^] - the Parent is your main form, the Child is your editform.

If you want to use the Show method, then you probably need to pass the data back to the main form via events (since Show doesn't wait for the user to finish, unlike ShowDialog): Transferring information between two forms, Part 2: Child to Parent[^] should help.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Apr-15 11:00am    
I tired to explain: those forms are not parents and not children. You are confusing inquirers. The whole idea to classify the solutions of this simple problem by role of the forms is wrong. It does not matter which of the forms is main, or owned, or modal, or anything, it's all about what one form "knows" about another one and how.
Sorry.
—SA
As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA
 
Share this answer
 

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