2015-04-01 135 views
0

我有两种形式,Form1和Form2。如何将数据从子窗体传递给父窗体

Form1中 - 家长 窗体2 - 儿童

Form1中包含以下,

文本框 - 它加载的文件路径, 的DataGridView - 加载其数据的文件, ButtonNext - 当按钮cliked它打开窗体2,

窗体2包含以下,

BrowseButton - 它从目录broswe的文件Textbox - 它然后显示路径 ButtonFinish - 它会将您重新映射到Form1

*现在我想从Form2(子)从Form1(父)访问datagridview。现在我可以broswe Form2上的文件,当我点击完成时,我可以看到我的文件路径Form1(父)从文本框,但没有databeing加载。

如何将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(); 

Form1中

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

可能重复http://stackoverflow.com/questions/4887820/how-do-you-通过对象从窗体1到窗体2和回到窗体1) – Dmitry 2015-04-01 12:55:05

+0

它看起来重复,但不同的问题 – prosts 2015-04-01 12:57:35

回答

0

首先第一件事情。请避免重复的帖子。

什么是变量_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; 
     } 
    } 
} 

干杯

的[你如何通过从Form1以Form2的对象并返回到Form1?](
+0

哟!...你救了我的生命,我已经坚持了这几天。 谢谢! – prosts 2015-04-01 14:36:16

相关问题