如何将数据从子窗体传递到父窗体

How to pass data from childform to parentform

我有两个表格,Form1 和 Form2。

Form1 - Parent 表格 2 - Child

Form1 包含以下内容,

文本框 - 它加载文件路径, Datagridview - 它加载文件及其数据, ButtonNext - 当点击按钮时它打开 Form2,

Form2 包含以下内容,

BrowseButton - 它从目录中浏览文件 文本框 - 然后显示路径 ButtonFinish - 它会让你回到 Form1

*现在我想从 Form1(Parent) 从 Form2(child) 访问 datagridview。现在我可以浏览 Form2 上的文件,当我单击完成时,我可以从文本框中看到我在 Form1(parent) 上的文件路径,但没有加载任何数据。

如何将 Form1 上的数据加载到 datagridview 中?

到目前为止,这是我的代码..

Form2.

    public frmInputFile(frmMain_Page _frmMain)
    {
        InitializeComponent();
        this._frmMain = _frmMain;
    }

private void btnBrowse_Click(object sender, EventArgs e)
 {
     BrowseFile();
}

 private void btnFinish_Click(object sender,EventArgs e)
    {

        _frmMain.SetFilepath(txtInputfile.Text);
        _grid.Rows.Clear();          //cant get the grid from form1
        string PathSelection = "";
        if (txtInputfile.Text.Length > 0)
        {
            PathSelection = txtInputfile.Text;
        }
        oDataSet = new DataSet();
        XmlReadMode omode = oDataSet.ReadXml(PathSelection);

        for (int i = 0; i < oDataSet.Tables[2].Rows.Count; i++)
        {
            string comment = oDataSet.Tables["data"].Rows[i][2].ToString();
            string font = Between(comment, "[Font]", "[/Font]");
            string datestamp = Between(comment, "[DateStamp]", "[/DateStamp]");
            string commentVal = Between(comment, "[Comment]", "[/Comment]");
            string[] row = new string[] { oDataSet.Tables["data"].Rows[i][0].ToString(), oDataSet.Tables["data"].Rows[i][1].ToString(), font, datestamp, commentVal };
            _grid.Rows.Add(row);
        }
       this.Hide();
        Program._MainPage.Show();

表格 1

    private void btnLoadfile_Click(object sender, EventArgs e)
    {
        frmInputFile frmInput = new frmInputFile(this);
        frmInput.Show();

    }
    public void SetFilepath(string Filepath)
    {
        txtInputfile.Text = Filepath;
    }
    //I dont know how i can handle the gridview here
    public void Loadgrid(string LoadGrid)
    {
        Gridview_Input.ToString();
    }

要事第一。请避免重复的帖子。

变量 _grid 在这里做什么??您将数据从一种形式传递到另一种形式的方式看起来很奇怪。不过,我尝试模拟您的问题,并且能够从 From2 添加行到 Form1 中。代码清单如下。需要注意的是,我在设计器中的 DataGridView 中添加了四列。在您的情况下,您可能还想以编程方式添加列。

 public partial class Form2 : Form
{
    public Form2(Form1 frm1)
    {
        InitializeComponent();
        Form1Prop = frm1;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form1Prop.SetFilepath("HIHI");

        Form1Prop.DataGridPropGrid.Rows.Add("HIH", "KI", "LO", "PO");
    }

    public Form1 Form1Prop { get; set; }
}




 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2(this);
        frm2.Show();
    }

    public void SetFilepath(string filepath)
    {
        textBox1.Text = filepath;
    }

    public DataGridView DataGridPropGrid
    {
        get
        {
            return dataGridView1;
        }
    }
}

干杯