【问题标题】:Trying to refresh a datagrid on main form after closing form 2关闭表单 2 后尝试刷新主表单上的数据网格
【发布时间】:2014-05-22 01:20:07
【问题描述】:

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

在主窗体上,一个按钮(“添加”)单击事件显示一个带有字段的 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();
    }

}

}

添加武器形态:

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


}
}

【问题讨论】:

标签: c# sql wpf forms datagrid


【解决方案1】:

您有多种选择。它们都处理在子窗体中引用主窗体。

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

【讨论】:

  • 关于选项 3. For this.DataInserted();在子表单的 IF 语句中,我收到错误消息。说这不需要 0 次争论。
  • 是的.. 传递这个,null 给它
  • 这样称呼它。DataInserted(this, null)
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多