tags:

views:

100

answers:

1

How do I pass a row information from my class to a grid in the windows form of my application? The row information changes every now and then and I need to pass this updated information to the form

+3  A: 

You can expose an event in your class that the form class can subscribe to. When that event is triggered the form can update the UI as needed. For example:

class ChildForm : Form
{
    public event EventHandler TextChanged;

    public string NewText { get { return textBox1.Text; } }

    void textBox1_TextChanged( object sender, EventArgs e )
    {
        EventHandler del = TextChanged;
        if( del != null )
        {
            del( this, e );
        }
    }
}

class MainForm : Form
{  
    void Foo( )
    {
        using( ChildForm frm = new ChildForm )
        {
            frm.TextChanged += (object sender, EventArgs e) => { label1.Text = frm.NewText; };
            frm.ShowDialog( );
        }
    }
}

You could actually just pass the TextBox.TextChanged event right no through in this example.

Ed Swangren
In my case ChildForm is not a form. Can I still do something like this?
Bi
Yes, that is just an example, the concept is the same.
Ed Swangren