如何将数据从子窗体传递到父窗体 [英] How to pass data from childform to parentform
问题描述
我有两种形式,Form1和Form2。
Form1 - 家长
Form2 - 儿童
Form1包含以下内容,
文本框 - 它加载文件路径,
Datagridview - 它加载文件及其数据,
ButtonNext-当按钮cliked时打开Form2,
Form2包含以下内容,
浏览按钮 - 它broswe目录中的文件
Textbox - 然后显示路径
ButtonFinish - 它会将你回到Form1
*现在我想从Form2(子)访问Form1(Parent)中的datagridview。现在我可以在Form2上浏览文件,当我点击完成后,我可以从文本框中看到Form1(父级)上的文件路径,但没有加载数据库。
如何我可以将Form1上的数据加载到datagridview吗?
这是我的代码到目前为止..
Form2。
I have two forms ,Form1 and Form2.
Form1 - Parent
Form2 - Child
Form1 Contains the following,
Textbox - it loads the file path,
Datagridview - it loads the file with its data,
ButtonNext -when button cliked it opens Form2,
Form2 Contains the following,
BrowseButton - it broswe for the file from the directory
Textbox - it then shows the path
ButtonFinish - it will tabes you back to Form1
*Now i want to access datagridview from Form1(Parent) from Form2(child). Now i can broswe the file on Form2 and when i click finish i can see my file path on Form1(parent) from the textbox but with no databeing loaded.
How can i load the data on Form1 into the datagridview ?
this is my code so far..
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();
}
推荐答案
我建议你阅读:一次回答的许多问题 - Windows窗体或WPF Windows之间的协作 [ ^ ]和:在两种表格之间转移信息,第1部分:父母对孩子 [ ^ ]
I'd suggest to read this: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows[^] and this: Transferring information between two forms, Part 1: Parent to Child[^]
这篇关于如何将数据从子窗体传递到父窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!