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

时间:2014-04-09 03:47:05

标签: c# sql wpf forms datagrid

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

在主窗体上,一个按钮(“添加”)单击事件,该事件显示带有字段的form2,以将新数据输入到主窗体中的表中。输入数据后,再单击一个按钮(“插入/添加”)点击事件,关闭form2并在主表单数据网格中显示新输入的数据。

问题是我不知道数据网格是如何刷新或更新以显示新信息的。任何帮助将不胜感激。

主要表格:

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();
    }

}

}

ADDWEAPON FORM:

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 个答案:

答案 0 :(得分:0)

您有几种选择。所有这些都涉及在子形式中引用主要形式。

选项#1:

将Main表单实例作为子表单中的构造函数传递:

在主窗体代码中:

    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();
}

答案 1 :(得分:0)

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

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