在C#中的表单之间传递网格视图数据

时间:2015-09-13 09:47:41

标签: c# sql .net visual-studio

我有两种形式。

表单#1有人员详细信息:姓名,电话,国家/地区等

表单#2有一个显示人员列表的网格视图。

当双击行时,如何从表单#2中的网格视图传递数据行以形成#1的控件?

1 个答案:

答案 0 :(得分:4)

请按照以下步骤操作。

  1. 创建一个类(我的演示中的clsGlobal)并声明对Form1和Form2的两个公共静态引用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Stack1
    {
    /// <summary>
    /// These references are now can be accessed any where within the    solution.
    /// </summary>
        public class clsGlobal
        {
            public static  Form1 frm1;
            public static Form2 frm2;
        }
    }
    
  2. clsGlobal.cs

    1. 如果此Form1是您的起始表单,则编辑Program.cs。如果不是没有必要。如果您愿意,您可以对您的起始表单执行此操作。我这样做是因为我的演示文稿中的Form Form是Form1。

      static class Program
      {
          /// <summary>
          /// The main entry point for the application.
          /// </summary>
          [STAThread]
          static void Main()
          {
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
      
              //Use the global reference for form1
              clsGlobal.frm1 = new Form1();
              Application.Run(clsGlobal.frm1);
          }
      }
      
    2. Program.cs

      1. 然后设计Form1和Form2。在Form1中,将所有TextBox的所有修饰符设置为Private to Public,以使其可供其他类访问(此处为Form2)。为此,请转到TextBox的Properties,然后将Modifiers属性的值更改为Public
      2. Form1 Design

        1. 在Form2中选择DataGridView,然后转到其属性 - &gt;事件并选择&#34; RowHeaderMouseDoubleClick&#34;来自活动清单的活动。
        2. Form2 Design

          1. 在这里将数据添加到我的Demo中的DataGrid中我手动创建了一个DataTable并将其设置为DataGrid的DataSource,但是您可以跳过此步骤,因为您已经在DataGridView中拥有了数据。 p>

            public Form2()
            {
                InitializeComponent();
                //You may don't need to do this part. You may can fetch the data from the database
                /////////////////////// To Disaplay Data On the DataGrid /////////////////
                DataTable dt = new DataTable();
                dt.Columns.Add("Name");
                dt.Columns.Add("Phone");
                dt.Columns.Add("Country");
            
                dt.Rows.Add("Supun", "+940711288825", "Sri Lanka");
                dt.Rows.Add("Nimantha", "+940783193677", "Sri Lanka");
            
                dataGridView1.DataSource = dt;
                ////////////////////////////////////////////////////////////////////////
            
                // To avoid select multiple rows at once
                dataGridView1.MultiSelect = false;
            
            } 
            
          2. 完成&#34; RowHeaderMouseDoubleClick&#34; Form2事件

            private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                //We know surely if this event fired there will be one selected row for sure
                //It is in the 0th index in the collection of SelectedRows
            
                //To access these textbox controls of form 1 inside form 2 you have to set 
                //their Modifiers to Public
                // We use the same instance of the form1 which is already opened
            
                 clsGlobal.frm1.txtName.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                 clsGlobal.frm1.txtPhone.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                 clsGlobal.frm1.txtCountry.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
            
                //clsoe the Form2
                 this.Close();
            }
            
          3. Form2.cs

            1. 在Form1中单击相应按钮的事件(In Demo btnOpenGridForm),执行如下操作

              private void btnOpenGridForm_Click(object sender, EventArgs e)
              {
                  //Use global reference for Form2
                  clsGlobal.frm2 = new Form2();
                  //You can't access Form1 now. if you want use .Show() instead of .ShowDialog()
                  clsGlobal.frm2.ShowDialog();
              }
              
            2. Form1.cs

              1. 此解决方案经过测试并为我工作,但我们可以为此提供替代解决方案。
              2. Before RowHeader Double Click

                After RowHeader Double Click