0

I am not sure how to proceed with refreshing the data grid view after I update and insert values into my database. I know that I have to bind the database values to the other form so that they will fill my DGV in the other form with the new content when I click the update or insert button.

so far this is what I have

this is my code for populating the Datagrid:

private void PopulateDataGrid()
    {

        Form1 f1 = new Form1();

        MySqlCommand cmd = conn.CreateCommand();
        DataTable datatable = new DataTable();

        cmd.CommandText = "select id,platenumber,brand,model,yearmodel,regdate,exdate,odometer from vehicle";
        cmd.CommandType = CommandType.Text;

        dataAdapter = new MySqlDataAdapter(cmd);
        dataAdapter.Fill(datatable);
        f1.dataGridView1.DataSource = datatable;

    }

but when I call the function in my save button it seems to have skipped it.

private void savebtn_Click(object sender, EventArgs e)
        {

            Form1 f1 = new Form1();
            DataTable dt = new DataTable();

           int newid = Convert.ToInt32(idtxt.Text);

           int ID = newid;

            MySqlCommand cmd = new MySqlCommand("update vehicle set platenumber=@platenumber, brand=@brand, model=@model, yearmodel=@yearmodel, regdate=@regdate, exdate=@exdate, odometer=@odometer where ID = @id" , conn);

            cmd.Parameters.AddWithValue("@id", ID);
            cmd.Parameters.Add("@platenumber", MySqlDbType.VarChar, 10).Value = pnumber.Text;
            cmd.Parameters.Add("@brand", MySqlDbType.VarChar, 60).Value = brand.Text;
            cmd.Parameters.Add("@model", MySqlDbType.VarChar, 45).Value = model.Text;
            cmd.Parameters.Add("@yearmodel", MySqlDbType.Int32).Value = yearmodel.Text;
            cmd.Parameters.Add("@regdate", MySqlDbType.Date).Value = datereg.Value;
            cmd.Parameters.Add("@exdate", MySqlDbType.Date).Value = regexp.Value;
            cmd.Parameters.Add("@odometer", MySqlDbType.Decimal).Value = odometer.Text;

            int i = cmd.ExecuteNonQuery();
            if (i != 0)
            {
                MessageBox.Show("Success");
            }
            else
            {   
                MessageBox.Show("Fail");
            }


            PopulateDataGrid();

            

            this.Close();
        }

I am confused about what I should do to refresh the DGV once I click a button to update/ insert in my database and close the separate form.

VinceG
  • 77
  • 1
  • 2
  • 10
  • Obviously, you did not read my comments from your last question about this same thing… [Why is my datagrid not updating after I update my database?](https://stackoverflow.com/questions/67398618/why-is-my-datagrid-not-updating-after-i-update-my-database) … the line of code… `Form1 f1 = new Form1();` … in both methods is your problem. You never display those forms so your code is doing work on a form that no one will ever see. This is why you do not see the changes. – JohnG May 11 '21 at 18:00
  • Hi sorry, I did read your comment on my previous post. I did add the `f1.Show();` before `this.Close();` and it worked. I saw the changes to my DGV. However, 2 of my Form1 are now open once I finish updating/ inserting. I just need 1 Form1 to be displayed after I update/ insert which is the previous Form that opened the Edit/ insert form. – VinceG May 12 '21 at 05:32
  • I am aware of what the `f1.Show()` would do, and that was my point, the code that is updating the grid in both methods is using their own 2nd `Form1` and this is different from the `Form1` you are seeing. I thought this would make things clear as to “why” you are not seeing your changes. Not showing the 2nd `Form1` changes nothing. The code is still referencing a “different” `Form1` than the one you are seeing. – JohnG May 12 '21 at 06:00
  • If you want `Form2` to reference something in `Form1` then you have to pass something to `Form2` when `Form1` creates and shows `Form2.` This is what my previous link in your previous question demonstrates… how to communicate/pass data between two forms. – JohnG May 12 '21 at 06:01
  • hey thanks man, i finally got it @JohnG – VinceG May 12 '21 at 10:05

1 Answers1

0

Slightly confused about what you are asking for but refresh(); should update your datagridview

  • I want my DGV to refresh once the edit/ insert form is closed. I have tried placing `f1.Refresh();` before `this.Close();` but it still wouldn't refresh my DGV – VinceG May 11 '21 at 10:58