Reputation: 13206
I need some help. I've been working with a colleague, trying to refresh datagridview on form1 each time form 2 is closed. Unfortunately, none of the previous questions he has asked on have been helpful in working towards our solution because we're both new to C#. As a result, I've opted to copy and paste the entirety of our code here.
We're not too sure what goes into public void RefreshGridView
(to refresh datagridview1 on form1) and how to have it run from the form2 closing action i.e. private void Form2_FormClosed
.
Form 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()
{
}
}
}
Form 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)
{
}
}
}
Upvotes: 1
Views: 312
Reputation: 216293
You can simply capture the close event of Form2 from Form1.
It is really easy
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
}
Upvotes: 4
Reputation: 5305
You should raise an event
in Form2
when it is closing in either FormClosing
or FormClosed
events. Then your Form1
should subscribe to that event and handle it. In the event handler you can then refresh your grid view.
Refer to this article in MSDN for further information about events: http://msdn.microsoft.com/en-us/library/awbftdfh.aspx
Upvotes: 0