0
votes

I have 2 forms.

On form1 there is a textbox called textbox1 and a button [and many other textboxes as well]

On clicking the button , a new form , form2 opens

form 2 has a datagridview with 2 columns.

On clicking the datagridview cell [present in form2] Using :

private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) , The contents of the first column of the selected row must go to the textbox1 in form1 , without refreshing or reopening form1 .

How do I do that without using the .show() method as it would refresh my form1 and as a result lose all the user-typed info in the other textboxes?

Note: retrieval from datagridview to string form is done by:

dataGridView.SelectedRows[0].Cells[1].Value.ToString()
2
Either pass an instance of Form1 to Form2 to be able to modify Form1's controls directly, or make events in Form2 which Form1 subscribes to and updates the controls when the events are raised. - Visual Vincent
Should the second form close after selecting the value? It seems you are goring to create a lookup form to select a value from a list. - Reza Aghaei
@VisualVincent I am a new programmer and a slow one too. I did not understand what you meant...sorry.. - No Holidays
@reza That is exactly what I want - No Holidays
My first suggestion was that you pass a variable to Form2 when opening it, where you set the variable to Form1 like: myNewForm2.Form1Instance = this; (this is Form1 if you open Form2 from there). -- My second suggestion was to create an event. An example of an event is Button1_Click when you click a button called Button1. You even have an event in your question: dataGridView_CellMouseClick. You can read about creating events on MSDN. - Visual Vincent

2 Answers

2
votes

Put that cell value in a public property like

public string gridcellValue
{
  get; set;
}

In your dataGridView_CellMouseClick( event handler set the property saying

this.gridcellValue = dataGridView.SelectedRows[0].Cells[1].Value.ToString();

Then in your form1 you can access it using the instance of form2 (since you created the form2 instance from form1)

textbox1.Text = frm2.gridcellValue;

Another approach is make that textbox a public property and pass the form1 instance in form2 constructor and set the textbox like

In Form1

public TextBox Form1Text
{
  get {return this.testbox1;} 
  set {this.testbox1 = value;}
}

Form2 frm2 = new Form2(this);
frm2.Show();

In Form2

public class Form2 : Form
{
  Form _form1;
public Form2(Form1 frm)
{
  _form1 = frm;
}

//In event handler

private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
   //set the value
   ((TextBox)_form1.Form1Text).Text = dataGridView.SelectedRows[0].Cells[1].Value.ToString();
}
}
0
votes

I solved it and I think my answer could help someone in need[like I was]

Basically , in Form1 , you create a public static string ,like

public static string mystring = "";

then on the button which leads you to Form2 , do the following:

private void button_Click(object sender, EventArgs e) {

       Form2 frm = new Form2();

        if (frm.ShowDialog() == DialogResult.OK)
        {
            this.txt_box.Text = Form1.mystring;

        }
    }

`

In form2,:

`

private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            Form1.mystring = dataGridView.SelectedRows[0].Cells[1].Value.ToString();
            this.DialogResult = DialogResult.OK;
            this.Close();


        }

And that is it! good luck