Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There are two forms.In first form there is a textbox and a picture box.When picture box is clicked it opens the second form which contains a datagridview.What i want is when i clicked on the cell of a datagridview its value should be shown in the textbox of first form.I do not want to create object of first form in second form because data of grid views has to be passed to multiple forms

What I have tried:

I have created an object of first form in second form but this is not the way i want
Posted
Updated 31-May-16 3:43am

This is reduced to the question related to form collaboration; by some reason, a very popular one.

As my previous answers on the topic often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA
 
Share this answer
 
Try this

Create a TextBox property in Form2 as
C#
public partial class Form2 :Form 
{
public TextBox ParentFormTextBox { get; set; }
public Form2()
.
.

before showing the Form2, initialize the above property with the Current (Form1) Form's TextBox as
C#
Form2 objForm2 = new Form2();
objForm2.ParentFormTextBox = this.textBox_Whatever;
objForm2.Show();

and assign the values of the datagridview cell to the Parent Form's TextBox using CellClick event as

C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
      {
          if (e.RowIndex > -1 && e.ColumnIndex > -1)
          {
             string value = Convert.ToString( dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
             ParentFormTextBox.Text = value;
          }
      }
 
Share this answer
 
Comments
gshashi 1-Jul-16 11:03am    
Karthik what if more than one form of type form1 exists
Karthik_Mahalingam 1-Jul-16 11:37am    
It will throw compile error
does it work?
gshashi 3-Jul-16 8:09am    
Then how to implement this
Karthik_Mahalingam 3-Jul-16 8:14am    
Try my solution
It will work
Form1 is of type Form

its been a month, still are u facing issue ?

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