首页 文章

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

提问于
浏览
-1

我有两种形式 .

表格#1有人员详细信息:姓名,电话,国家等 .

表格#2具有显示人员列表的网格视图 .

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

1 回答

  • 4

    请按照以下步骤操作 .

    • 创建一个类(我的演示中的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;
        }
    }
    

    clsGlobal.cs

    • 如果此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);
        }
    }
    

    Program.cs

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

    Form1 Design

    • 在Form2中选择DataGridView,然后转到其属性 - >事件,并从事件列表中选择"RowHeaderMouseDoubleClick"事件 .

    Form2 Design

    • 在这里我的Demo中将数据添加到我的DataGrid中我手动创建了一个DataTable并将其设置为DataGrid的DataSource,但是您可以跳过此步骤,因为您已经拥有DataGridView中的数据 .
    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;
    
    }
    
    • 完成Form2的“RowHeaderMouseDoubleClick”事件
    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();
    }
    

    Form2.cs

    • 在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();
    }
    

    Form1.cs

    • 此解决方案经过测试并为我工作,但我们可以为此提供替代解决方案 .

    Before RowHeader Double Click

    After RowHeader Double Click

相关问题