Click here to Skip to main content
15,126,209 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am developing a application in Visual Studio 2010 c#

I have two forms as shown in below image

ScreenShot Link

here in Form2 I have DataGridView control with user name and in Form1 I have a TextBox and a Button. I opened Form2 by

Form2 frm = new Form2();

my query is how I can get the selected column value of GDataGridView in Form1 TextBox?

Hello Ashok ,

You have to use constructor to this.

try this link

Passing Data Between Forms

First of all for passing value from one form to another we need to use Constructor,
here in your scenario you have 2 forms
1) having textbox and button.
2) having the datagridview.

now when you click the button from form1 ,form2 pop you want to select value from datagridview and display that value in text box of form1.

for achieving this you just need to do 2 things.
1) create a parameterised constructor in form1 which will ask a string paramter like that.

public Form1(string val)
         TextBox1.Text = val;

2) on the form2 fire "datagrid_CellClick" event

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
           string val=dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
           Form1 ob = new Form1(val);

this event will return you row index which is clicked and column index you have you will get the value you have selected and pass that value to the constructor of form1 for displaying it.
Sai Kumar 1-Apr-14 8:03am
hai i have 2 forms form1 has textbox,button-(when i click on this button it redirects to form2 which has a datagridview and button(btnok))and when i double click on cell or btnok(button) of datagridview i want to get data of that cell value into form1 textbox(txtpubcode).
Sai Kumar 2-Apr-14 3:24am
when I tried the above code the value is getting into form2 but here iam getting the error
"object reference not set to an instance of an object" why iam getting that
Hi all,

Found correct answer with all your suggestion.

Thank you for your help.

Here is my working code for all needy people.

In Form1

private void BtnSelect_Click(object sender, EventArgs e)
            textBox1.Text= frm._textBox1.ToString();
        public string _textBox
            set { textBox1.Text = value; }

and in Form2
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            string val = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            textBox1.Text = val;
        public string _textBox1
            get { return textBox1.Text.Trim(); }


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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900