更新或插入另一个窗体上时,C#刷新的DataGridView [英] C# refresh DataGridView when updating or inserted on another form
问题描述
我有2个形式,它们是 A型
和 B型
,
A型
允许用户插入,更新学生信息。
和表b
,它只是一个DataGridView和按钮那里,
当我在形成A
,然后我去 B型
,新的学生没有在DataGridView中显示出来,如果我重新运行该程序,新的学生将可以出现在 b型
。
我试着表b用这个按钮
datagridview1.refresh();
datagridview1.update();
但仍然没有工作,任何人都可以帮助我吗? 。请
编辑:
我的插入代码工人
CMD =新的OleDbCommand(插入FWINFOS(ID,姓名,性别,出生日期,种族的工作场所,PassportNO,DateOfExpire [位置],照片)VALUES('+ textBox5.Text +,+ textBox1.Text +,+ textBox2.Text +,+ dateTimePicker1.Value +',' + textBox3.Text +,+ textBox4.Text +,+ textBox6.Text +,+ dateTimePicker2.Value +',@位置,@照片),CON);
cmd.Parameters.AddWithValue(@位置,comboBox1.SelectedText.ToString());
conv_photo();
con.Open();
INT N = cmd.ExecuteNonQuery();
//cmd.ExecuteNonQuery();
con.Close();
如果(N 0)
{
MessageBox.Show(插入);
loaddata();
RNO ++;
}
,否则
MessageBox.Show(不插入);
}
我Datagridview1(窗体2 )当我插入一个新的工作不会自动更新。但如果我重新运行应用程序,新的工作出现。
///表一个
公共无效loaddata()
{
////你在加载数据以更新数据网格
数据做什么}
然后在表格b定义:
//表格b
FORMA OBJ =(FORMA)Application.OpenForms [备考];
私人无效的button1_Click(对象发件人,EventArgs五)
{
obj.loaddata();
datagridview1.update();
datagridview1.refresh();
}
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.
/// 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();
}
这篇关于更新或插入另一个窗体上时,C#刷新的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!