Click here to Skip to main content
16,021,417 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

C#
Form2 frm = new Form2();
        frm.ShowDialog();


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

Hello Ashok ,

You have to use constructor to this.

try this link

Passing Data Between Forms

thanks
 
Share this answer
 
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 ups.now 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.

C#
public Form1(string val)
     {
         TextBox1.Text = val;
     }



2) on the form2 fire "datagrid_CellClick" event

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

this event will return you row index which is clicked and column index you have selected.now you will get the value you have selected and pass that value to the constructor of form1 for displaying it.
 
Share this answer
 
Comments
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

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

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


cheers..!!!!!!!!!
 
Share this answer
 

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