Click here to Skip to main content
14,329,523 members
Rate this:
Please Sign up or sign in to vote.
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();
        frm.ShowDialog();


my query is how I can get the selected column value of GDataGridView in Form1 TextBox?
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

Hello Ashok ,

You have to use constructor to this.

try this link

Passing Data Between Forms

thanks
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.

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);
           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.
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 3

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)
        {
            frm.ShowDialog();
            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;
            this.Close();
        }
        public string _textBox1
        {
            get { return textBox1.Text.Trim(); }
        }


cheers..!!!!!!!!!
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100