Hi All

I am looking for a way to pass certain columns from a datagridview to a previous form. The columns i want passed is clientname, contactdetails, email, cellphonenumber, telephonenumber and faxnumber.

How it works is as follow:

In my form I have some read-only textboxes. Above them I have a button which takes me to the client form, but keeps the previous form open. In my client form I want to select a row from the datagridview and pass the column values mentioned above to the previous forms textboxes.

This is my code so far:

private void addClientToJobBtn_Click(object sender, EventArgs e)
        {
            try
            {
                //Reading the selected row in the current datagrid and then passing it on to the user edit form
                String ClientName = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[1].Value.ToString();
                String ContactPerson = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[2].Value.ToString();
                String CellPhoneNumber = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[3].Value.ToString();
                String TelephoneNumber = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[4].Value.ToString();
                String FaxNumber = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[5].Value.ToString();
                String EmailAddress = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[6].Value.ToString();
                AddJob aj = new AddJob(this, ClientName, ContactPerson, CellPhoneNumber, TelephoneNumber, FaxNumber, EmailAddress);
                aj.Show();
                this.Close(); 
            }
            catch (ArgumentOutOfRangeException err)
            {
                MessageBox.Show("Select a client first");
            }
        }

It doesn't work though..How can I make it return to the previous form, pass the values and also close the current form?

Any help please??

Hi,

I did not understood what you need about your forms, can you try to explain again.
About showing and hideing the forms, if you need to only hide the first form, show the second one and after finished with it close it and return to the first form you need to do this:

AddJob aj = new AddJob();
this.Hide();
aj.ShowDialog();
this.Show();

If you dont need to retrun to the first form so your code is OK.
Tell me what you need about the data and i will help you.

Okay sorry for the little info i gave..let me explain:

I have a from called add job. In that form I have a button that will take me to the client form. On that form I have a datagridview. The user will select a row in the datagridview and then there's a button called "addClientToJobButon". This button must take certain columns in that selected row and pass it to the previous form called add job. Every column passed on will be displayed in their own text box.

My problem is I don't seem to get back to the previous form. Instead a new instance of add job opens up.

Hope you understand now. Thanks for the reply :)

Pass values by the class constructor:

Form that you want to open:

...

string arg;

public className(argumentThatYouWantToPass) {
    InitializeComponents;
    
    arg = argumentThatYouWantToPass;
}
...

Form that will open another form:

public void openForm() {
    Form2 form = new Form2(argument);
    
    form.Show();
}

Pass values with the class constructor:

Form that you want to open:

...

string arg;

public className(argumentThatYouWantToPass) {
    InitializeComponents;
    
    arg = argumentThatYouWantToPass;
}
...

Form that will open another form:

public void openForm() {
    Form2 form = new Form2(argument);
    
    form.Show();
}

Edited 5 Years Ago by jugosoft: n/a

Okay so this makes sense :)

But just one stupid question. How do I pass columns in my datagridview to textboxes on my previous form? As you see on my first post here you will see I do a similar action to what you gave now..

If you want to pass all that data, that can be done by passing an array or passing all that variables.

If you decide to use an array, you must split your data with something. Here's the code:

using System.Text.RegularExpresions;

...

public string[] convertToArray(argument1, argument2, argument3, ...) 
{
    string temporaryElemenets = argument1 + ";" + argument2 + ";" + argument3 + ";" + ... ;
    
    // ; is character that will split data. Eg. We have string 'abc; cba'
    // function will return array with values {ABC, CBA}
    return Regex.Split(temporaryElemenets, ";");
}

Edited 5 Years Ago by jugosoft: n/a

Putting all together how will I go about doing this (Where do I start)? Sorry if I sound stupid and stuff but ya this is the first time I deal with this type of problem.

I didn't understand you. Do you have a problem with passing data or you want to select and pass all data from datagrid?

I did a very basic simple example of passing data to a previos form:

//form1:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonForm2_Click(object sender, EventArgs e)
        {
            //be careful to use "this" keyWord in here, becuase its passed to the Form2 constructor!
            Form2 form2 = new Form2(this); 
            form2.Show(this);
        }

        public void GetDataFromForm2(string[] array)
        {
            string name = array[0];
            string lastName = array[1];
            textBox1.Text = name;
            textBox2.Text = lastName;
        }
    }

    //form2:
    public partial class Form2 : Form
    {
        Form1 form1;
        public Form2(Form1 _form1)
        {
            InitializeComponent();
            form1 = _form1;
        }

        private void buttonPassToForm1_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            string lastName = textBox2.Text;
            if (name != String.Empty && lastName != String.Empty)
            {
                string[] array = new string[2] { name, lastName };
                form1.GetDataFromForm2(array);
            }
        }
    }

In the code you open form2 form form1. There are textBoxes on both forms. But what you will write into them on form2, with pressing the button (buttonPassToForm1) the data will be passed to textBoxes on Form1.

I get an error:

An object reference is required for the non-static field, method, or property 'Job_Book_Application.AddJob.GetDataFromForm2(string[])'

And lies here (In my case):

AddJob.GetDataFromForm2(array);

Edited 5 Years Ago by TheDocterd: n/a

There is a way you can do it when you close form2. In form1, make a new eventhandler that handles form.close():

form2.FormClosing += new FormClosingEventHandler(yourEventHandler);

yourEventHandler code should be in form1:

partial class form1: form
  {
  ...
  private void yourEventHandler(object sender, EventArgs e)
  {
      //here is where you get the data stored in the form2, which is 
      //the last thing that will happen just before the form (form2) closes.
      //for instance:

      myForm1Textbox.Text = myForm2Instance.getEnteredText();
    
      //and here we assume that myForm2Instance.getEnteredText() returns text
      //stored  in some field within form2.
  }
  ...
}

to recap, here's what happens:
1. while running form1, you instantiate form2.
2. you enter data in form2.
3. you do something that triggers form2 to close.
4. your form-closing eventhandler is called.
5. in this case, the form-closing eventhandler got some text stored in some field in form2.

Hope this helps! It's hard to understand just what your problem is, but I think this should do it if you haven't already fixed it.

This article has been dead for over six months. Start a new discussion instead.