关闭表单2后,正在尝试刷新主表单上的数据网格

本文关键字:表单 刷新 数据 网格 数据网 | 更新日期: 2023-09-27 17:57:26

我有一个数据网格问题,并且在添加新数据时让它刷新。我试图让它发挥作用的方式是。

在主窗体上,一个按钮("添加")单击事件显示一个带有字段的窗体2,用于将新数据输入到主窗体中的表中。一旦输入了数据,然后一个按钮"插入/添加"单击事件关闭窗体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 Microsoft.Win32;
namespace WindowsFormsApplication2
{
public partial class Main : Form
{

    public Main()
    {
        InitializeComponent();
    }
    private void Main_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'userLoginDataSet.WeaponData' table. You can move, or remove it, as needed.
        this.weaponDataTableAdapter.Fill(this.userLoginDataSet.WeaponData);
    }
    private void panel1_Paint(object sender, PaintEventArgs e)
    {
    }
    private void pictureBox1_Click(object sender, EventArgs e)
    {
    }
    private void button1_Click(object sender, EventArgs e)
    {

        AddWeapon aw = new AddWeapon();
        aw.Show();
    }
}
}

添加武器形式:

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.SqlClient;
using System.Data.Sql;
using Microsoft.Win32;
using System.Threading;
namespace WindowsFormsApplication2
{
public partial class AddWeapon : Form
{
    public AddWeapon()
    {
        InitializeComponent();
    }
    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.'SQLEXPRESS;AttachDbFilename=C:'Users'brmcbrid'Documents'Visual Studio 2010'Projects'WindowsFormsApplication2'WindowsFormsApplication2'UserLogin.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
        SqlCommand cmd = new SqlCommand("INSERT into WeaponData values('" + serialNumber.Text + "','" + brand.Text + "','" + model.Text + "','" + caliber.Text + "','" + type.Text + "' , '" + dateAcquired.Text + "', '" + dateSold.Text + "', '" + purchasePrice.Text + "', '" + sellPrice.Text + "', '" + notes.Text + "')", con);

        this.Close();
    }

}
}

关闭表单2后,正在尝试刷新主表单上的数据网格

您有几个选项。所有这些都涉及到在子窗体中引用主窗体

选项1:

将主窗体实例作为子窗体中的构造函数传递:

主表单代码:

    AddWeapon aw = new AddWeapon(this); // pass this, the main form
    aw.Show();

在子窗体中,有一个主窗体的私有字段和一个额外的构造函数。

public partial class AddWeapon : Form
{
    private Main _mainForm;
    public AddWeapon()
    {
     InitializeComponent();
     }
     public AddWeapon(Main mainForm) : this()
     {
      this._mainForm = mainForm;
     }
     // remaining code.
     private void button1_Click(object sender, EventArgs e)
     {
      SqlConnection con = new SqlConnection(@"Data Source=.'SQLEXPRESS;AttachDbFilename=C:'Users'brmcbrid'Documents'Visual Studio 2010'Projects'WindowsFormsApplication2'WindowsFormsApplication2'UserLogin.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
      SqlCommand cmd = new SqlCommand("INSERT into WeaponData values('" + serialNumber.Text + "','" + brand.Text + "','" + model.Text + "','" + caliber.Text + "','" + type.Text + "' , '" + dateAcquired.Text + "', '" + dateSold.Text + "', '" + purchasePrice.Text + "', '" + sellPrice.Text + "', '" + notes.Text + "')", con);
      // call a public method on the main form that can update the data.
      this._mainForm.UpdateData();
      this.Close();
     }
  }

选项2:

您也可以通过子窗体的公共属性设置它,而不是在构造函数中传递主窗体的引用,并执行同样的操作。

    AddWeapon aw = new AddWeapon();
    aw.Main = this;
    aw.Show();

选项#3:

此选项没有表单实例连接。您所做的是在子窗体插入数据并让父窗体订阅此事件时引发事件。

以父形式

    AddWeapon aw = new AddWeapon();
    aw.OnDataInserted += this.DataInserted;
    aw.Show();

以子形式,

public event EventHandler DataInserted;

然后在插入之后

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(@"Data Source=.'SQLEXPRESS;AttachDbFilename=C:'Users'brmcbrid'Documents'Visual Studio 2010'Projects'WindowsFormsApplication2'WindowsFormsApplication2'UserLogin.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    SqlCommand cmd = new SqlCommand("INSERT into WeaponData values('" + serialNumber.Text + "','" + brand.Text + "','" + model.Text + "','" + caliber.Text + "','" + type.Text + "' , '" + dateAcquired.Text + "', '" + dateSold.Text + "', '" + purchasePrice.Text + "', '" + sellPrice.Text + "', '" + notes.Text + "')", con);
    if (this.DataInserted != null)
    {
     this.DataInserted();
    }
    this.Close();
}

您可以在主窗体的激活事件上编写代码来刷新网格,如下所示:

    private void Main_Activated(object sender, EventArgs e)
    {
         // write your code here
    }