0

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();
    }
prosts
  • 141
  • 2
  • 2
  • 10
  • possible duplicate of [How do you pass an object from form1 to form2 and back to form1?](http://stackoverflow.com/questions/4887820/how-do-you-pass-an-object-from-form1-to-form2-and-back-to-form1) – Dmitry Apr 01 '15 at 12:55
  • It looks duplicate but with different question – prosts Apr 01 '15 at 12:57

1 Answers1

0

First things first. Please avoid duplicate posts.

What is the variable _grid doing here ?? Your way of passing data from one form to the other looks very strange. Nevertheless, I tried to simulate your problem and I am able to add the rows in Form1 from From2. The code listing is given below. Just a thing to note,I have added four columns in to my DataGridView in the designer. In your case you might want to add the columns as well programmatically.

 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;
        }
    }
}

Cheers