【问题标题】:passing grid view data between forms in C#在 C# 中的表单之间传递网格视图数据
【发布时间】:2015-09-13 09:47:41
【问题描述】:

我有两种形式。

表格 #1 包含人员详细信息:姓名、电话、国家/地区等。

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

当双击该行时,如何将表格#2 中网格视图中的数据行传递给表格#1 的控件?

【问题讨论】:

  • 不要尝试在网格之间传递行,而是将相关数据从第一个表单传递到第二个表单。有多种方法可以做到这一点 - 使用第二种形式的构造函数,或者它的自定义属性,或者方法。表单只是一个对象,因此就像将数据从foo 传递到bar
  • 请问可以写代码吗

标签: c# sql .net visual-studio


【解决方案1】:

请按照以下步骤操作。

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

  1. 如果此 Form1 是您的起始表单,则编辑 Program.cs。如果不是没有必要。如果您愿意,您可以对您的起始表格执行此操作。我这样做是因为在我的演示中我的起始表单是 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);
        }
    }
    

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

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

  1. 在此处将数据添加到我的 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;
    
    } 
    
  2. 完成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();
    }
    

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

  1. 此解决方案已经过测试并适用于我,但我们可能会为此提供替代解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2014-06-18
    • 1970-01-01
    • 2015-05-20
    相关资源
    最近更新 更多