1
votes

I open a datagrid when I double-click the textbox in my form1. I need to give two clicks on a cell of datagrid in form2 and when I click it already bring value to the textbox in form1. I've tried many ways no longer works, which he already ran automatically adds the value in the textbox before I click the cell, can anyone help?

I tried this:

in form1

         Ncm Ncm formNcm = new ();
         formNcm.Show ();
         ncmcb.Text formNcm.getNCM = ();
         ncmcb.Focus ();

the form2

public string getNCM () { = dataGridView1.CurrentCell.Value.ToString value2 string (); return value2; } Only this way it selects already own the value of the datagrid and I can not change, if I change it does not take the value for the textbox in form1

1
Can you please clarify it further, unfortunately it's very hard to understand what you mean :( - Beenish Khan
I want to pass a value to a datagrid textbox in another form - Carlos Mendez
So you have Form1 and Form2. From form1 , you open Form2 and you want to pass a value ? Also, is it a web application or windows ? - Beenish Khan
Yes, pass a value from form1 to form2, must select the data from datagrid and move to the textbox in another form. The desktop application is - Carlos Mendez

1 Answers

0
votes

Design wise, one form should never know that other form has a textbox or drop down or some other control. Ideally the two forms should interact via public properties or custom events.

Assigning Value from Form2 to Form1

  1. Define a custom event in Form 2.

  2. Make the Form1 register with that event, so that when Form 2 fires it, Form1 can catch it.

  3. Capture the grid cell click event in Form2 and fire the custom event. You will be using EventArgs to pass the value.

  4. In Form1, event handler should get in action and there you can assign the value you sent from Form 2.

Here's an article doing exactly that : http://www.codeproject.com/Articles/17371/Passing-Data-between-Windows-Forms

if you want to assign a value from Form2 to Form1,

In Form 2 , define a property eg.

publis string MyTextBoxValue //Choose a better name :)
{
     set {myTextBox.Text = value;}
}

And when you open the form, you make the call in following manner,

     Ncm Ncm formNcm = new ();
     formNcm.MyTextBoxValue = "Grid Cell Value"; //Whatever your value is
     formNcm.Show ();
     ncmcb.Text formNcm.getNCM = ();
     ncmcb.Focus ();