Click here to Skip to main content
12,702,312 members (35,613 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked

How to get a value from a child form back to a caller form

, 5 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
how to get gridview selected row value to text box which is in another windows form


This simple article explains how you can get a value on a different form on to a caller form.


Basically you may keep a method to access the values in the child form window. Using a GET method is better than exposing the entire object to outside.

Using the code

Firstly, you need to have a method in the child form to get the values. 

public DataGridViewSelectedRowCollection getselectedrow()
    return dataGridView1.SelectedRows;

Some example code to show how the child form is instantiated and the method being called:

public partial class Form1 : Form
    public Form1()
    Form2 form2show;
    private void button1_Click(object sender, EventArgs e)
        if ((form2show==null) || (form2show.IsDisposed==true))
            form2show = new Form2();
    private void button2_Click(object sender, EventArgs e)
        if ((form2show==null) || (!form2show.Visible))
        DataGridViewSelectedRowCollection dsr = form2show.getselectedrow();
        if ((dsr == null) || (dsr.Count == 0))

        textBox1.Text = dsr[0].Cells[1].Value.ToString();

Points of Interest

It's always better to use a method to access properties like above. We call them GET methods. It's recommended than exposing an entire object to outside.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pasan Indeewara
Software Developer (Senior)
Sri Lanka Sri Lanka
• C# Developer since 2005
• SAP/ABAP Technical Consultant since Sep. 2010
• Has experience in .NET Framework, SQL, VFP

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 5 Jul 2012
Article Copyright 2012 by Pasan Indeewara
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid