从子窗体刷新父窗体的数据网格


//Adding Data to Database Here.      
cmd = new SqlCommand("INSERT INTO itemDB(brname, itmname, itmunit,itmgr, itmmrp, itmbyp, itmdlrp, itmtx, itmdlrmrg, itmrtmrg, itmusrcode, active) VALUES (@label11, @label4, @label6, @label14, @label2, @label9, @label10, @label8, @label12, @label13, @label7, @label3)", con);
cmd.Parameters.Add("@label11", combobrand.GetItemText(combobrand.SelectedItem));
cmd.Parameters.Add("@label4", itemname.Text);
cmd.Parameters.Add("@label6", combouom.GetItemText(combouom.SelectedItem));
cmd.Parameters.Add("@label14", itemkgs.Text);
cmd.Parameters.Add("@label2", itemmrp.Text);
cmd.Parameters.Add("@label9", itembrp.Text);
cmd.Parameters.Add("@label10", itemslp.Text);
cmd.Parameters.Add("@label8", itemtax.Text);
cmd.Parameters.Add("@label12", itemdlmargin.Text);
cmd.Parameters.Add("@label13", itemretailmargin.Text);
cmd.Parameters.Add("@label7", itemcode.Text);
cmd.Parameters.Add("@label3", status);
cmd.ExecuteNonQuery();
combobrand.SelectedIndex = -1;
combouom.SelectedIndex = -1;
itemname.Text = "";
itemunit.Text = "";
itemmrp.Text = "";
itembrp.Text = "";
itemslp.Text = "";
itemtax.Text = "";
itemdlmargin.Text = "";
itemretailmargin.Text = "";
itemcode.Text = "";
MessageBox.Show("Record Saved", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Close();

此表单用于添加数据。按下关闭按钮后,我想刷新我的父表单数据网格。

将数据加载到表单 1 的数据网格

Public void dataload()
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM[dbo].              [itemDB]", con);
DataTable dt = new DataTable();
sda.Fill(dt);
foreach (DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = item["itmcode"].ToString();
dataGridView1.Rows[n].Cells[1].Value = item["brname"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["itmname"].ToString();
dataGridView1.Rows[n].Cells[3].Value = item["itmunit"].ToString();
dataGridView1.Rows[n].Cells[4].Value = item["itmgr"].ToString();
dataGridView1.Rows[n].Cells[5].Value = item["itmml"].ToString();
dataGridView1.Rows[n].Cells[6].Value = item["itmpc"].ToString();
dataGridView1.Rows[n].Cells[7].Value = item["itmtx"].ToString();
dataGridView1.Rows[n].Cells[8].Value = item["itmbyp"].ToString();
dataGridView1.Rows[n].Cells[9].Value = item["itmdlrmrg"].ToString();
dataGridView1.Rows[n].Cells[10].Value = item["itmrtmrg"].ToString();
dataGridView1.Rows[n].Cells[11].Value = item["itmdlrp"].ToString();
dataGridView1.Rows[n].Cells[12].Value = item["itmmrp"].ToString();
dataGridView1.Rows[n].Cells[13].Value = item["itmusrcode"].ToString();
dataGridView1.Rows[n].Cells[14].Value = item["active"].ToString();
}
con.Close();
}

在表单 2 中添加数据后,窗体 1 数据网格不刷新。为 刷新我在form1中添加了一个新按钮,然后单击该按钮i 正在刷新数据网格的数据。 我创建的用于将数据加载到数据网格的数据加载函数。我在打电话 这个函数在我需要的地方。

因此,首先,您需要使父窗体中的 datagridView 成为公共窗体,并通过以下代码覆盖用于添加数据的窗体上的 OnClosed 事件:

protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
foreach (var item in Application.OpenForms)
{
DataSet dataSet = new DataSet();
using (SqlConnection connection= new SqlConnection(ConnectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter(new SqlCommand(SelectStatement,connection));
adapter.Fill(dataSet);
}
if (item.GetType()==typeof(parentForm))
{
(item as Form1).dataGridView1.DataSource = dataSet.Tables[0];
}
}
}

由于您几乎没有提供任何代码,因此我假设您有"父表单"Form1打开Form2

Form2使用您提供的命令插入数据。

一种方法是将Form2显示为对话框表单(位于另一个表单之上的表单,您无法在其下方单击)。当该窗体成功关闭(记录保存到数据库中)时,刷新父窗体上的网格 (Form1)。

因此,在Form2(子窗体)上,插入数据后,设置窗体的DialogResult,如下所示:

//... removed previous statements for brevity
MessageBox.Show("Record Saved", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Close();
//record is inserted, now return DialogResult "OK", so that the parent form can refresh grid
//this also closes this child, insert form
this.DialogResult = DialogResult.OK;

在父窗体上,打开子窗体后,应检查子窗体的DialogResult。如果该结果正常,请刷新网格。 在父窗体上,代码应如下所示:

Form2 frmInsert = new Form2();
DialogResult dr = frmInsert.ShowDialog();
if (dr == DialogResult.OK)
{
dataGridView1.Rows.Clear();
dataload();
}
else
{
//nothing, user didn't insert anything on insert form
}

另一种方法是从子窗体触发事件。这允许您根据需要显示子窗体(不一定是对话框窗体),只需等待事件触发,以便您可以刷新网格。

在子窗体上添加名为RefreshNeededEventHandler,如下所示(在窗体的范围内)

public partial class Form2 : Form
{
//event handler
public EventHandler RefreshNeeded;
public Form2()
{
InitializeComponent();
}
//... rest of form's code...

在子窗体 (Form2) 上,插入数据时触发事件(假设在btnInsert_Click上):

private void button1_Click(object sender, EventArgs e)
{
//... removed previous statements for brevity
MessageBox.Show("Record Saved", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Close();
//after data is saved, fire event!
RefreshNeeded?.Invoke(this, new EventArgs());
}

在父窗体上,您必须订阅事件,并添加触发事件时执行的方法:

Form2 frmInsert = new Form2();
//subscribe to event
frmInsert.RefreshNeeded += new EventHandler(RefreshGrid);
frmInsert.Show();
//....
private void RefreshGrid(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
dataload();
}

应该就是这样了。如果您有一些问题,请随时询问。

最新更新