更新3个表单之间的数据库 [英] update database between 3 forms?

查看:51
本文介绍了更新3个表单之间的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

这可能有问题:

我有3种形式:

表格1有数据网格

表单2是登录表单

表单3显示要修改的详细信息。

当我双击表单1的datagrid时,需要登录才能修改。 />
在表格3上修改后,我该如何表格1是来自表格3的更新数据?

请帮助我!

谢谢

Hi all!
This is may problem :
I have 3 forms :
form 1 has a datagrid
form 2 is a login form
form 3 shows details to modify.
When I double click on datagrid of form 1, login is needed to modify.
After modify on form 3, How can I do that form 1 is update data from form 3?
Pleas help me!
Thanks

推荐答案

从表单3中触发事件,类似于 DataUpdated 。处理形式1并采取相应行动。





~~~~~~~~



在回复您的评论时,这里有一些示例代码显示了我的意思(您应该能够将此模式应用到您的项目中):



Fire an event from form-3, something like DataUpdated. Handle this in form-1 and act accordingly.


~~~~~~~~

In response to your comment, here's some sample code that shows what I mean (you should be able to adapt this pattern into your project):

class Form1 : Form
{
    Form3 form3 = new Form3(); // form3 instance

    void LoadInternal()
    {
        form3.DataUpdated += Form3_DataUpdated;
    }

    void Form3_DataUpdated(object sender, EventArgs e)
    {
        ModifyData();
    }

    void ModifyData()
    {
        Form2 login = new Form2();
        if (login.ShowLogin())
        {
            ModifyInternal();
        }
    }

    private void ModifyInternal()
    {
        // update the grid
    }

}

class Form2 : Form
{
    internal bool ShowLogin()
    {
        // prompt for auth details
        return true;
    }
}

class Form3 : Form
{
    public event EventHandler DataUpdated;

    private void FireDataUpdated()
    {
        var temp = this.DataUpdated;

        if (temp != null)
        {
            temp(this, EventArgs.Empty);
        }
    }

    public void Foo()
    {
        // some code that updates details

        FireDataUpdated();
    }
}







~~~~~ ~~~

在回复您的评论时,这是您在 Form3 类中定义的事件。它是从 Form1 处理的。这是一种非常流行的方法,用于Winforms中的表格间通信。





~~~~~~ ~~

为了回应您的评论, var 只是隐式类型局部变量的快捷方式。在我的代码示例中,它将在编译时解析为 EventHandler 。看到你对C#很新,这对你来说可能听起来很奇怪。但我这样做的原因是线程安全。由于委托是不可变的,并且由于事件的后备存储是委托,因此您可能会遇到竞争条件,即使另一个线程正在触发这些事件,事件处理程序也会在一个线程中取消挂钩。为避免这种情况,使用局部变量来创建副本。现在只需忽略它并直接使用代码。




~~~~~~~~
In response to your comment, it's an event that you define on your Form3 class. And it's handled from Form1. This is a very popular approach that's used for inter-form communication in Winforms.


~~~~~~~~
In response to your comment, var is just a shortcut for implicitly typed local variables. In my code example, that will resolve to EventHandler at compile-time. Seeing that you are rather new to C#, that may sound odd to you. But the reason I did that is for thread safety. Since delegates are immutable, and since the backing store for an event is a delegate, you may run into a race condition where an event handler is unhooked in one thread even as another thread is firing those events. To avoid this situation, a local variable is used to create a copy. For now just ignore that and use the code directly.


这篇关于更新3个表单之间的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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