locked
Update a parent form from child form RRS feed

  • Question

  • Hi everyone!

    My app:

    Form1: I search a Project in a DB (table tblProjects). I have a button that allows me to see the products within such project in Form2.

    Form2: Shows products within the project (table tblDetails). I have a button that shows me a Form3, where I can show details of a specific item of the product list. It also has a datagridview that everytime I select a specific product within this project shows me the related details of that project (reads it from table tblProduct)

    Form3: Shows details of a specific product (table tblProduct). I can update the details of each product, such as name, etc.

    Now, everything seems to work ok, except when I update a product in Form3. I close Form3 after I changed something, but the datagridview in Form2 does not show me the update unless I restart my app .

    Any ideas? thank you!

    Monday, March 7, 2011 2:41 PM

Answers

  • I did an example code for you. I use only 2 forms. Form1 has a dgv (with 2 columns; id and pruduct name), which is bound to dataTable. With a button click (and a row selected) the nre form2 opens. In my example I have put two textBoxes, id and priduct name. You can change the name (you can NOT id), and with a button click the dgv on Form1 is updated (and the form2 closes).

    I think this is what you wanted. Here is the code:

    //form1:
      public partial class Form1 : Form
      {
        BindingSource bs;
        DataTable table;
        public Form1()
        {
          InitializeComponent();
          table = new DataTable("myTable");
          table.Columns.Add("id", typeof(int));
          table.Columns.Add("product", typeof(string));
    
          //lets do some example populations:
          DataRow dr;
          for (int i = 1; i < 6; i++)
          {
            dr = table.NewRow();
            dr["id"] = i;
            dr["product"] = "Product " + i;
            table.Rows.Add(dr);
          }
          bs = new BindingSource(table, null);
          dataGridView1.DataSource = bs;
          dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          if (dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Index > -1)
          {
            try
            {
              object[] array = new object[dataGridView1.Columns.Count];
              foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                  array[i] = row.Cells[i].Value.ToString();
    
              Form2 form2 = new Form2(this, array);
              form2.Show(this);
            }
            catch
            {
              MessageBox.Show("There has been an error. Please try again, select only one row in dgv.");
            }
          }
          else
            MessageBox.Show("Please choose the row in dgv.");
        }
    
        public void UpdateDataTable(object[] obj)
        {
          DataRow[] row = table.Select("id = '" + obj[0].ToString() + "'");
          row[0]["product"] = obj[1].ToString();
        }
      }
    
    //form2:
      public partial class Form2 : Form
      {
        Form1 form1;
        public Form2(Form1 _form1, object[] args)
        {
          InitializeComponent();
          form1 = _form1;
    
          //LETS SAYS THAT 1ST TEXTBOX (ID) CANNOT BE EDITABLE -SO ITS READ ONLY!!
          //THIS IS IMPORANT TO HAVE AN ID FOR THE DATATABLE, OTHERWISE YOU CANNOT UPDATE IT!
    
          textBox1.Enabled = false;
          textBox1.Text = args[0].ToString();
          textBox2.Text = args[1].ToString();
        }
    
        private void buttonUpdate_Click(object sender, EventArgs e)
        {
          object[] array = new object[2] { textBox1.Text, textBox2.Text };
          form1.UpdateDataTable(array);
          this.Close();
        }
      }
    

     

    PS: Please take time to examine the code. Do not rush. Otherwise you will surely miss sometihng. Remember, code works, the dgv gets updated. I was even took care of possible error, so the code is more or less optimized.

    Hope it helps, if not let me know.

    Mitja

    • Marked as answer by juacochero Tuesday, March 8, 2011 12:10 AM
    Monday, March 7, 2011 3:58 PM

All replies

  • You can use delegates to update the controls on other form.

    btw, is your dgv on form2 data bound (maybe to dataTable, which you have mentioned there)? Or how do you populatge dgv? 

    Monday, March 7, 2011 3:25 PM
  • Thanks for your response, but could you be more specific on the delegates? Im quite new to this.

    the dgv on Form2 is databound to the tblProduct.

    thanks!

    Monday, March 7, 2011 3:32 PM
  • I did an example code for you. I use only 2 forms. Form1 has a dgv (with 2 columns; id and pruduct name), which is bound to dataTable. With a button click (and a row selected) the nre form2 opens. In my example I have put two textBoxes, id and priduct name. You can change the name (you can NOT id), and with a button click the dgv on Form1 is updated (and the form2 closes).

    I think this is what you wanted. Here is the code:

    //form1:
      public partial class Form1 : Form
      {
        BindingSource bs;
        DataTable table;
        public Form1()
        {
          InitializeComponent();
          table = new DataTable("myTable");
          table.Columns.Add("id", typeof(int));
          table.Columns.Add("product", typeof(string));
    
          //lets do some example populations:
          DataRow dr;
          for (int i = 1; i < 6; i++)
          {
            dr = table.NewRow();
            dr["id"] = i;
            dr["product"] = "Product " + i;
            table.Rows.Add(dr);
          }
          bs = new BindingSource(table, null);
          dataGridView1.DataSource = bs;
          dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          if (dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Index > -1)
          {
            try
            {
              object[] array = new object[dataGridView1.Columns.Count];
              foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                  array[i] = row.Cells[i].Value.ToString();
    
              Form2 form2 = new Form2(this, array);
              form2.Show(this);
            }
            catch
            {
              MessageBox.Show("There has been an error. Please try again, select only one row in dgv.");
            }
          }
          else
            MessageBox.Show("Please choose the row in dgv.");
        }
    
        public void UpdateDataTable(object[] obj)
        {
          DataRow[] row = table.Select("id = '" + obj[0].ToString() + "'");
          row[0]["product"] = obj[1].ToString();
        }
      }
    
    //form2:
      public partial class Form2 : Form
      {
        Form1 form1;
        public Form2(Form1 _form1, object[] args)
        {
          InitializeComponent();
          form1 = _form1;
    
          //LETS SAYS THAT 1ST TEXTBOX (ID) CANNOT BE EDITABLE -SO ITS READ ONLY!!
          //THIS IS IMPORANT TO HAVE AN ID FOR THE DATATABLE, OTHERWISE YOU CANNOT UPDATE IT!
    
          textBox1.Enabled = false;
          textBox1.Text = args[0].ToString();
          textBox2.Text = args[1].ToString();
        }
    
        private void buttonUpdate_Click(object sender, EventArgs e)
        {
          object[] array = new object[2] { textBox1.Text, textBox2.Text };
          form1.UpdateDataTable(array);
          this.Close();
        }
      }
    

     

    PS: Please take time to examine the code. Do not rush. Otherwise you will surely miss sometihng. Remember, code works, the dgv gets updated. I was even took care of possible error, so the code is more or less optimized.

    Hope it helps, if not let me know.

    Mitja

    • Marked as answer by juacochero Tuesday, March 8, 2011 12:10 AM
    Monday, March 7, 2011 3:58 PM
  • One more thnig: the code in Form1`s constructor create dataTable and populates it with some example data. You do not need that. You get data from database (of somewhere else, doesnt metter now). But you need to have a Global variable DataTable (so you can access to it in whole Form1 class).

    Mitja

    Monday, March 7, 2011 4:00 PM
  • Thank you Mitja, your code helped a lot.

    It seems that the client now wants it with textboxes instead of datagridviews, so i'll try to adapt your code accordingly.

    Thank you so much for your time!

    Tuesday, March 8, 2011 12:10 AM