gpt4 book ai didi

c# - 跨不同表单使用 datagridview

转载 作者:太空宇宙 更新时间:2023-11-03 21:37:38 24 4
gpt4 key购买 nike

我需要一些帮助。我一直在和一位同事一起工作,试图在每次关闭表单 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com