0

so I have this refresh and populate function here

private void Refresh()
{
    MySqlCommand cmd = conn.CreateCommand();
    String data, id, platenumber, brand, model, yearmodel, odometer;

    cmd.CommandText = "SELECT * FROM vehicle";
    cmd.CommandType = CommandType.Text;

    MySqlDataReader reader = cmd.ExecuteReader();

    while (reader.Read())
    {
        id = reader.GetInt32(0).ToString();
        platenumber = reader.GetString(1);
        brand = reader.GetString(2);
        data = $"{ id}/{ platenumber}/{ brand}";
    }

    reader.Close();

    PopulateDataGrid();
}

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;
}

and this is my update function

private void savebtn_Click(object sender, EventArgs e)
{
    Form1 f1 = new Form1();

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

    int ID = newid;

    MySqlCommand cmd = new MySqlCommand("update vehicle set platenumber=@platenumber 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.MinDate;
    //cmd.Parameters.Add("@exdate", MySqlDbType.Date).Value = regexp.MinDate;
    cmd.Parameters.Add("@odometer", MySqlDbType.Decimal).Value = odometer.Text; 

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

    f1.Refresh();

    this.Close();
}

the problem is when I click on the save button on FORM2 everything on the datagrid stays the same but when I click a refresh button from FORM1 with the same code it works. even if I put the function in FORM2 it still doesn't refresh the datagrid after I click the update button. what am I missing here?

uvr
  • 515
  • 4
  • 12
VinceG
  • 77
  • 1
  • 2
  • 10
  • The DGV doesn't automatically repaint. The trick is to set to null : f1.dataGridView1.DataSource = null; f1.dataGridView1.DataSource = datatable; – jdweng May 05 '21 at 09:30
  • The client side will display whatever was retrieved last. Update to the DBMS will only show when you retrieve or bind again. - Imagine how inconvenient automatic updating would be for an unsuspecting user! Hint: Always display the time and date of the rerieved data! – TaW May 05 '21 at 09:31
  • In both the `PopulateDataGrid` and `savebtn_Click` methods there is a line of code… `Form1 f1 = new Form1();` ? … This looks odd and without question ANY controls on `f1` may well be updated, however the form is NEVER displayed to the user. Therefore, the updates are probably happening, its just that you can not see them since `f1` is NEVER displayed. How many `Form1`’s are you wanting to display? In both methods… add the line of code… `f1.Show();` … and you will see the problem. – JohnG May 05 '21 at 14:57
  • You may want to take a look at how to pass data between two `Forms` … [Communicate between two windows forms in C#](https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp) – JohnG May 05 '21 at 15:07

0 Answers0