每次关闭form2时,在form1上刷新datagridview1

时间:2013-12-29 21:30:53

标签: c# winforms datagridview

每次关闭form2时,如何在form1上刷新 datagridview1

即。 refreshDataGridView1()需要运行,但我不太清楚如何初始化它。

refreshDataGridView1()需要了解什么?

private void save_btn_Click(object sender, EventArgs e)
{

    if (pgpText.Text.Trim().Length == 0)
    {
        MessageBox.Show("Please fill the following textbox: PGP");
    }
    else if (teamText.Text.Trim().Length == 0)
    {
        MessageBox.Show("Please fill the following textbox: Team");
    }
    else
    {

        using (OleDbConnection conn = new OleDbConnection())
        {
            string pgp_new = pgpText.Text;
            string pgp_old = pgpOld.Text;
            string team = teamText.Text;
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='db.mdb'";
            OleDbCommand command = new OleDbCommand();
            command.Connection = conn;
            command.CommandText = "UPDATE PGP SET PGP=?,Team=? WHERE PGP=?";
            command.Parameters.Add("pgp_new", OleDbType.VarChar).Value = pgp_new;
            command.Parameters.Add("team", OleDbType.VarChar).Value = team;
            command.Parameters.Add("pgp_old", OleDbType.VarChar).Value = pgp_old;
            conn.Open();

            int affectedRows = (int)command.ExecuteNonQuery();

            if (affectedRows == 0)
            {
                command.CommandText = "INSERT INTO PGP (PGP,Team) VALUES (?, ?)";
                command.Parameters.RemoveAt(2);
                command.ExecuteNonQuery();
                if (MessageBox.Show("Table Saved!", "Update", MessageBoxButtons.OK) == DialogResult.OK)
                {
                    //refreshDataGridView1();
                    this.Close();
                }
            }

            if (affectedRows > 0)
            {
                if (MessageBox.Show("Table Saved!", "Update", MessageBoxButtons.OK) == DialogResult.OK)
                {
                    //refreshDataGridView1();
                    this.Close();
                }
            }

        }
    }
}

5 个答案:

答案 0 :(得分:1)

使用OnFormClosing的{​​{1}}事件,并在该事件中调用您的方法Form2

修改

在您的refreshDataGridView1()方法中重新绑定网格,如

refreshDataGridView1()

答案 1 :(得分:1)

我建议你在Form1中创建一个函数,并在Form2(Form_Closing)中退出时调用它。我的意思是当Form2关闭时,调用该函数。该函数应该更新您的datagridview。

如何访问该功能?好吧,您可以轻松地将传递给Form2,同时创建:

Form2 frm2 = new Form2(this);

在你的Form2中:

private Form1 frm1;

public Form2(Form1 frm1)
{
    this.frm1 = frm1;
}

private void Form2_Form_Closing(...)
{
    this.frm1.UpdateDataGridViewFunc();
}

答案 2 :(得分:1)

将Form2视为对话框。在Form1中移动MessageBox和UpdateDatagridview逻辑。每当您完成查询(在Form2中)时,请传递DialogResult.OK。它也会关闭你的表格。

窗体2:

if (affectedRows > 0)
{
     //ok some rows were affected, close the form and pass OK result
     this.DialogResult = DialogResult.OK;
}

回到Form1:

private void openForm2()
{
    var f2 = new Form2(); //create new form2
    var formResult = f2.ShowDialog(); //open as Dialog and check result after close event
    if (formResult == DialogResult.OK) //check form2 dialog result
    {
        //form2 gave OK, I can update my DGV and display msg
        MessageBox.Show("DGV will be updated", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //update my DGV
        UpdateDGV();
    }
    else
    {
        //form2 passed Cancel or something else, not good
        MessageBox.Show("Form2 Closed but nothing happened.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
}   

private void UpdateDGV() {
    //refresh datagridview1 datasource
}

答案 3 :(得分:0)

分配Form2_FormClosed事件:

this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form2_FormClosed);

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
refreshDataGridView1();
}

答案 4 :(得分:0)

DataGridView.DataSource = dataset; // get new result set

像这样的东西应该放在refreshDataGridView1()

要访问Form2中的Form1元素,请在Form1中添加一个公共属性。

public DataGridView DataGridView1 
{
    return dataGrid; // this should be your data grid id
}

在form2中,您可以将form1作为

访问
Form1 form1 = new Form1();
form1.DataGridView1.DataSource = dataSource;

如果一个表单包含另一个表单,则可以创建一个构造函数以将其传递给另一个。

private Form1 form1;
public Form2(Form1 form1)
{
    this.form1 = form1;
}