C#在更新或插入另一个表单时刷新DataGridView [英] C# refresh DataGridView when updating or inserted on another form
问题描述
我有2个表单,分别为表单A
和表单B
,
I have 2 forms which are form A
and form B
,
form A
允许用户插入和更新学生信息。
form A
allowes user to insert and update student information.
form b
只是一个DataGridView和按钮。
form b
is only a DataGridView and button there.
当我插入学生在表单A
,然后我去形式B
,新学生没有显示在DataGridView上,如果我重新运行该程序,新学生将出现在 form B
。
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 rerun the program, the new student will appear in form B
.
我试过在表单b上使用这个按钮
I tried using this on button on form b
datagridview1.refresh();
datagridview1.update();
但仍然无法使用。
已编辑:
我插入工作的代码
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");
}
我的 Datagridview1(Form2)
在插入新的工作者时不会自动更新。但是如果我重新运行应用程序,新的工作者将出现。
my Datagridview1(Form2)
doesn't automatically update when I inserted a new worker. But if I rerun the application, the new worker appears.
推荐答案
// Form A
public void loaddata()
{
//do what you do in load data in order to update data in datagrid
}
然后在Form B上定义:
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();
}
这篇关于C#在更新或插入另一个表单时刷新DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!