在c#中将数据从一个gridview传递到另一个gridview

时间:2014-03-22 03:50:16

标签: c# winforms row

我使用不同形式的两个网格视图,我想传递form1所有单元格值 另一个表格[form2]。 form2数据 在一个单元格中查看 。然后单击form2单元格加载form1单元格中的所有数据视图。

此表单1 - >

    private void button1_Click(object sender, EventArgs e)
    {

        string[] colB = new string[dataGridView1.Rows.Count];

        for (int i = 0; i < dataGridView1.Rows.Count -1; i++)
        {
            colB[i] = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);

        }
    }

如何获取数据form2-&gt; ?

1 个答案:

答案 0 :(得分:0)

表单就像其他任何类一样。但是,它们具有从Form类继承的特定于窗口的行为。

这意味着您可以指定自己的构造函数并传递您想要的任何信息。例如,假设我们要在新表单中显示Person类。我们可以定义一个新的PersonForm来显示详细信息(这只是一个示例。您将能够根据自己的需要进行调整。)

public class PersonForm : Form 
{
    PersonForm(Person model)
    {
        InitializeComponents(); //I think this is the name of the method.

        //Do whatever is needed here with the model.
    }
}

因此,当用户双击右侧单元格时,您将创建PersonForm的新实例并显示它。