本文介绍了在另一个表单上插入数据时更新主表单的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Windows表单应用程序,其中我使用了两个表单主表单新表单。在主表单中,有很多控件可用于显示通过新表单插入的数据库中的数据。每当我打开应用程序时,首先打开主窗体,其中先前的数据显示在datagridview和更多控件中。当我单击主窗体上可用的NewFormbtn以打开新窗体时,它将打开。现在我想将一些新数据插入数据库,主表单上的所有可用控件应在插入新数据时自动更新。请帮助

I am developing a Windows Form Application, where I used two Forms says Main Form and New Form. In the Main Form there is much controls are available to display data from database inserted through the New Form. Whenever I open the application, first the Main Form opens where previous data is displayed in datagridview and in more controls. When I click the NewFormbtn available on Main Form to open the New Form, it opens. Now I want to insert some new data to database, all the controls available on Main Form should automatically updated while inserting new data. Please help

推荐答案

public event EventHandler ModelUpdated;





然后将以下功能添加到新表格:





Then add the following function to New Form:

protected virtual void OnModelUpdated()
{
    EventHandler handlers = ModelUpdated;
    if (handlers != null)
        handlers(this, EventArgs.Empty);
}





现在,当您在主窗体中创建表单时,类似于:





Now when you create the form in the Main Form, something like:

public void CreateNewForm()
{
    NewForm newForm = new NewForm();
    newForm.ModelUpdated += new EventHandler(newForm_ModelUpdated);
}

private void newForm_ModelUpdated(object sender, EventArgs e)
{
    //Reload the data in the main form here
}





你可以看中这些东西,让你的拥有EventArgs类并告诉主表单您更新了哪些数据,或者您要传回的任何其他信息。



这是事件驱动的开发,是多重的基础-form systems。



You can get fancy with this stuff, make your own EventArgs class and tell the main form what data you updated, or any other information you want to pass back.

This is event driven development and is a fundamental of multi-form systems.


这篇关于在另一个表单上插入数据时更新主表单的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 00:55