Refresh datagridview1 on form1 each time form2 is closed

浪尽此生 提交于 2019-12-11 12:32:32

问题


How do I refresh datagridview1 on form1 each time form2 is closed?

i.e. refreshDataGridView1() needs to be run, but I'm not too sure how to go about initializing it.

What needs to go in 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();
                }
            }

        }
    }
}

回答1:


use OnFormClosing event of your Form2 and in that event call your method refreshDataGridView1()

EDIT:

In your refreshDataGridView1() method just rebind the grid like

private void refreshDataGridView1()
{
  GridView1.DataSource = <some data Source>;
}



回答2:


I suggest you to make a function in Form1 and call it upon exiting in Form2 (Form_Closing). I mean when Form2 is closing, call the function. The function should update your datagridview.

How can you access the function? Well, you can easily pass this to Form2 while creating:

Form2 frm2 = new Form2(this);

And in your Form2:

private Form1 frm1;

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

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



回答3:


Treat your Form2 as a Dialog. Move your MessageBox and UpdateDatagridview logic in Form1. Whenever you have finished your query (in Form2), pass DialogResult.OK. It will also close your form.

Form2:

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

Back in 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
}



回答4:


Assign the Form2_FormClosed event:

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

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



回答5:


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

Something like this should go in refreshDataGridView1()

For accessing Form1 elements in Form2 add a public property in Form1.

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

In form2, you can access form1 as

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

If one form contains another you can create a constructor to pass one to another.

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


来源:https://stackoverflow.com/questions/20830268/refresh-datagridview1-on-form1-each-time-form2-is-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!