【问题标题】:pass data from one gridview to another gridview in c#在c#中将数据从一个gridview传递到另一个gridview
【发布时间】:2014-03-22 12:52:24
【问题描述】:

我正在使用不同形式的两个网格视图,我想传递 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-> ?

【问题讨论】:

  • 我们无法从头开始编写代码,请向我们展示您目前所拥有的。
  • 通常表示复杂的数据,建议使用模型类,这样信息就与View(Grid)解耦了。如果要以这种方式组织代码,则可以简单地在两个窗口之间传递模型对象的引用。如果您在 WPF 中工作,请查找 MVVM 模式。如果您使用 Windows 窗体,请寻找 MVP (MVC) 模式。
  • 我建议创建新的静态类并为该类分配值并从 form2 访问该类。

标签: c# winforms row


【解决方案1】:

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

这意味着您可以指定自己的构造函数并传递您想要的任何信息。例如,假设我们有一个 Person 类,我们想在一个新的 Form 中显示。我们可以定义一个新的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 的新实例并显示它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 2019-05-08
    相关资源
    最近更新 更多