跨不同表单使用datagridview

时间:2014-01-02 22:14:54

标签: c# winforms datagridview

我需要一些帮助。我一直在与一位同事合作,每次关闭表单2时都尝试刷新form1上的datagridview。不幸的是,他之前提出的问题都没有帮助我们解决问题,因为我们都是C#的新手。因此,我选择在此复制并粘贴我们的全部代码。

我们不太清楚public void RefreshGridView(刷新form1上的datagridview1)以及如何从form2结束操作private void Form2_FormClosed运行它。

表单1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PGPTool
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'dbDataSet.PGP' table. You can move, or remove it, as needed.
        this.pGPTableAdapter.Fill(this.dbDataSet.PGP);
    }

    // new

    private void new_btn_Click(object sender, EventArgs e)
    {
        var cell1 = "";
        var cell2 = "";
        Form2 Form2 = new Form2(cell1, cell2);
        Form2.Show();
    }

    // clear

    private void clear_btn_Click(object sender, EventArgs e)
    {
        search_txt.Text = "";
    }

    // search

    private void search_btn_Click(object sender, EventArgs e)
    {
        searchData();
    }

    private void search_txt_TextChanged(object sender, EventArgs e)
    {
        searchData();
    }

    private void searchData()
    {
        BindingSource bs = new BindingSource();
        bs.DataSource = dataGridView1.DataSource;
        bs.Filter = "PGP like '%" + search_txt.Text + "%' or Team like '%" + search_txt.Text + "%'";
        dataGridView1.DataSource = bs;
    }

    // edit

    private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        var selectedRow = dataGridView1.CurrentRow;
        var cell1 = Convert.ToString(selectedRow.Cells[0].Value);
        var cell2 = Convert.ToString(selectedRow.Cells[1].Value);
        Form2 Form2 = new Form2(cell1, cell2);
        Form2.Show();
    }

    // copy to clipboard

    private bool isCellClicked = false;

    private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
    {
        var hit = dataGridView1.HitTest(e.X, e.Y);
        isCellClicked = (hit.Type == DataGridViewHitTestType.Cell);
    }

    private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
    {
        e.Cancel = !isCellClicked;
    }

    private void copyToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        Clipboard.SetText(Convert.ToString(dataGridView1.CurrentCell.Value));
    }

    // refresh grid

    public void RefreshGridView()
    {
    }

}
}

表格2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace PGPTool
{
    public partial class Form2 : Form
    {
        public Form2(string cell1, string cell2)
        {
        InitializeComponent();
        pgpText.Text = cell1;
        pgpOld.Text = cell1;
        teamText.Text = cell2;
    }

    private void pgpText_TextChanged(object sender, EventArgs e)
    {
        pgpText.CharacterCasing = CharacterCasing.Upper;
    }

    private void teamText_TextChanged(object sender, EventArgs e)
    {
        teamText.CharacterCasing = CharacterCasing.Upper;
    }

    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)
                    {
                        this.Close();
                    }
                }

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

            }
        }
    }

    private void cancel_btn_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private Form1 Form1Instance
    {
        get;
        set;
    }

    public Form2(Form1 form1Instance)
    {
        Form1Instance = form1Instance;
    }

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

    }
}

2 个答案:

答案 0 :(得分:4)

您可以从Form1中简单地捕获Form2的close事件 这真的很容易

private void new_btn_Click(object sender, EventArgs e)
{
    var cell1 = "";
    var cell2 = "";
    Form2 Form2 = new Form2(cell1, cell2);
    Form2.FormClosed += Form2HasBeenClosed;
    Form2.Show();
}
private void Form2HasBeenClosed(object sender, FormClosedEventArgs e)
{
    // Inside the form1 call your RefreshGridView
}

答案 1 :(得分:0)

eventForm2事件结束时,您应在FormClosing中提出FormClosed。然后你的Form1应订阅该事件并处理它。在事件处理程序中,您可以刷新网格视图。 有关事件的更多信息,请参阅MSDN中的这篇文章:http://msdn.microsoft.com/en-us/library/awbftdfh.aspx