Tham JunKai Tham JunKai - 25 days ago 8
C# Question

C# refresh DataGridView when updating or inserted on another form

I have 2 forms which are

form A
and
form B
,

form A
is allowed user to insert,update student information.

and
form b
, it's only a DataGridView and button there,

when i insert student on
form A
, then i go to
form B
, the new student did not show on the DataGridView , and if i re-run the program, the new student will be appear at
form B
.

i tried used this on button on form b

datagridview1.refresh();
datagridview1.update();


but still not working, anyone could help me? please.




Edited:

My code for inserting a worker

cmd = new OleDbCommand("insert into FWINFOS (ID,Name,Gender,DateOfBirth,Race,WorkingPlace,PassportNO,DateOfExpire,[Position],Photo) values('" + textBox5.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + dateTimePicker1.Value + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + dateTimePicker2.Value + "',@Position,@Photo)", con);


cmd.Parameters.AddWithValue("@Position", comboBox1.SelectedText.ToString());
conv_photo();

con.Open();
int n = cmd.ExecuteNonQuery();
//cmd.ExecuteNonQuery();
con.Close();
if (n > 0)
{
MessageBox.Show("Inserted");
loaddata();

rno++;
}
else
MessageBox.Show("No Insert");



}


my Datagridview1(Form2) doesn't automatically update when i inserted a new worker. but if i rerun the application, the new worker appear.

Answer
/// Form A
public void loaddata()
{
    ////do what you do in load data in order to update data in datagrid
}

then on Form B define:

// Form B
FormA obj = (FormA)Application.OpenForms["FormA"];

private void button1_Click(object sender, EventArgs e)
{
    obj.loaddata();
    datagridview1.update();
    datagridview1.refresh();
}
Comments