Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
Nish Nishant 13-Feb-11 23:14pm    
Updated my answer with some sample code based on your comment.
Nish Nishant 13-Feb-11 23:35pm    
Updated again.

BTW please post comments as responses, do not post answers. A moderator may delete these if you do that. Thank you.

1 solution

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

[Edit]
~~~~~~~~

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):

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


[Edit 2]
~~~~~~~~
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.

[Edit 3]
~~~~~~~~
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.
 
Share this answer
 
v4
Comments
Abhinav S 13-Feb-11 23:36pm    
Good answer mate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900