我需要一些帮助。我一直在与一位同事合作,尝试在每次关闭表单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)
    {
    }

    }
}

最佳答案

您可以简单地从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
}

关于c# - 使用不同形式的datagridview,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20892947/

10-17 02:15