子窗体关闭后更新DataGridView [英] Update DataGridView after Child Form Closed

查看:116
本文介绍了子窗体关闭后更新DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道很多类似的问题都在那里。但是,我真的很喜欢C#,所以不能弄清楚如何解决这个问题。



我在主窗体上有一个 DataGridView ,我有一个对话框来添加新记录。所以,我想要的是在对话框关闭时重新加载/刷新主窗体上的 DataGridView 。 (在对话框上按保存按钮)。



所以,我在这个主窗体上有一个公共方法,我使用这个方法:

  public void UpdateProductsList()
{
String query =SELECT * FROM product ;
con = new SqlConnection(conString);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(query,con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}

我使用这段代码打开子窗体:

  private void AddProductButton_Click(object sender,EventArgs e)
{
Add_product obj = new Add_product();
obj.ShowDialog();
}

当点击保存按钮时,我在Child表单上调用此方法。

  private void SaveProductButton_Click(object sender,EventArgs e)
{
SaveProduct();
产品产品=新产品();
products.UpdateProductsList();
}

但是,当点击保存按钮时,这不起作用。奇怪的是,当我将此方法添加到主窗体上的本地按钮时,它没有问题。



我还向 UpdateProductsList 添加了一个 MessageBox '确定它被调用,数据也被插入到数据库中,但是 DataGridView 不会显示新记录。


解决方案

当您使用显示子表单时, ShowDialog 不需要从子窗体调用 LoadData ,而可以检查 ShowDialog ,如果它是 DialogResult.OK ,调用该方法。



同样在您的子窗体中,在保存数据后的保存按钮中,设置 this.DialogResult = DialogResult.OK



显示子表格

 使用(var f = new ChildForm())
{
if(f.ShowDialog()== System.Windows.Forms.DialogResult.OK)
this.LoadData(); / *以列表形式加载数据* /
}

保存按钮在孩子表单

  this.SaveData(); / *保存子表单中的数据* / 
this.DialogResult = System.Windows.Forms.DialogResult.OK;

注意




  • ShowDialog 被调用,直到对话框关闭后才执行代码。

  • 如果您使用 ShowDialog ,设置 表单的对话框结构 属性关闭窗体。

  • 您可以在保存按钮中将 DialogResult 设置为 OK 并设置到取消在您的取消按钮。

  • 目前您的问题是在您的保存按钮的代码,您已经创建了一个新的列表形式,并调用其 UpdateProductsList 方法。它对您可以看到的列表形式的打开实例没有任何影响。这是一个不同的isntace。


I know lot of similar questions are out there. But, I'm really new to C# so, cannot figure out how to solve this.

I have a DataGridView on main form and I have a dialog box to add new record. So, what I want is to reload/refresh the DataGridView on main form when the dialog box closes. (Press Save button on dialog box).

So, I have a public method on the main form like this, which I load data using this method:

public void UpdateProductsList()
{
  String query = "SELECT * FROM product";
  con = new SqlConnection(conString);
  con.Open();
  SqlDataAdapter sda = new SqlDataAdapter(query, con);    
  DataTable dt = new DataTable();
  sda.Fill(dt);
  dataGridView1.DataSource = dt; 
}

I use this code to open the child form:

private void AddProductButton_Click(object sender, EventArgs e)
{
    Add_product obj = new Add_product();
    obj.ShowDialog();
}

Now I call this method on the Child form when the Save button is clicked.

private void SaveProductButton_Click(object sender, EventArgs e)
{
    SaveProduct();
    Products products = new Products();
    products.UpdateProductsList();
}

However, this is not working when the Save button is clicked. Strange thing is, when I add this method to a local button on the main form, it works without a problem.

I also added a MessageBox to UpdateProductsList and I'm sure it was called, also the data was inserted in database, but the DataGridView doesn't show new record.

So, where I've gone wrong ?

解决方案

When you show the child form using ShowDialog yo don't need to call LoadData from child form, instead you can check the result of ShowDialog and if it was DialogResult.OK, call the method.

Also in your child form, in your save button after saving data, set this.DialogResult = DialogResult.OK.

Show Child Form

using (var f = new ChildForm())
{
    if(f.ShowDialog()== System.Windows.Forms.DialogResult.OK)
        this.LoadData(); /*Load data in list form*/
}

Save Button in Child Form

this.SaveData(); /*Save Data in child form */
this.DialogResult = System.Windows.Forms.DialogResult.OK;

Note

  • When ShowDialog is called, the code following it is not executed until after the dialog is closed.
  • If you show a form using ShowDialog, setting DialogResult property of Form closes the form.
  • You can set DialogResult to OK in your save button and set it to Cancel in your cancel button.
  • Currently your problem is in code of your save button, you have created a new instance of your list form and called its UpdateProductsList method. It doesn't have any impact on the open instance of your list form which you can see. It's a different isntace.

这篇关于子窗体关闭后更新DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆