C# 更新或插入另一个表单时刷新 DataGridView [英] C# refresh DataGridView when updating or inserted on another form

查看:22
本文介绍了C# 更新或插入另一个表单时刷新 DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个表单,分别是 form Aform 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.

当我在 form A 上插入学生时,然后我转到 form 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();

但它仍然无法正常工作.

but it's still not working.

我插入工人的代码

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) 在我插入一个新的 worker 时不会自动更新.但是如果我重新运行应用程序,就会出现新的工作人员.

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
}

然后在表格 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆