如何将值从datagridview传递到另一个表单? [英] How do I pass values from datagridview to another form ?

查看:105
本文介绍了如何将值从datagridview传递到另一个表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个需要编辑表单的程序。此表单用于编辑datagridview中的国家/地区。然而,我对如何将数据从主窗体中的数据gridview传递到编辑表单上的相对位置感到困惑。我完成了大部分编码,如下所示。我知道我需要一个方法在我的AVL类中将所选国家/地区的所有信息返回到主表单方法,然后将其传递回编辑表单并显示它。如果有人可以帮我完成这个方法,我将不胜感激,我不确定该传递什么以及在这个方法中写些什么。



主表单方法



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

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

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方法





AVL Tree method

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;
        }

推荐答案

看看这个:在两种形式之间传递信息,第1部分:父母与子女 [ ^ ] - 父级是您的主要形式,子级是您的编辑形式。



如果你想使用Show方法,那么你可能需要通过事件将数据传回主表单(因为Show不等待用户完成,与ShowDialog不同):在两个表格之间传递信息,第2部分:孩子到父母 [ ^ ]应该有帮助。
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.

由于这个问题非常受欢迎,我以前的答案常常没有得到很好的理解,可能还不够清楚,我决定写一篇提示/技巧文章,详细的代码示例和解释:< a href =http://www.codeproject.com/Tips/889332/Many-Questions-Answered-at-Once-Collaboration-betw>一次回答的许多问题 - Windows窗体或WPF Windows之间的协作 。



-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


这篇关于如何将值从datagridview传递到另一个表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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