每次form2关闭时,在form1上刷新datagridview1 [英] Refresh datagridview1 on form1 each time form2 is closed

查看:216
本文介绍了每次form2关闭时,在form1上刷新datagridview1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



refreshDataGridView1()需要运行,但我不太确定如何初始化它。



需要在 refreshDataGridView1()

  private void save_btn_Click对象发件人,EventArgs e)
{

if(pgpText.Text.Trim()。Length == 0)
{
MessageBox.Show(请填写以下文本框:PGP);
}
else if(teamText.Text.Trim()。Length == 0)
{
MessageBox.Show(请填写以下文本框:Team);
}

{

using(OleDbConnection conn = new OleDbConnection())
{
string pgp_new = pgpText.Text;
string pgp_old = pgpOld.Text;
string group = 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();
}
}

}
}
}


解决方案

使用 OnFormClosing 您的事件> Form2 在这种情况下调用您的方法 refreshDataGridView1()



编辑:



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

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


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();
                }
            }

        }
    }
}

解决方案

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>;
}

这篇关于每次form2关闭时,在form1上刷新datagridview1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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