C#: Passing data to forms UI using BeginInvoke

Posted by Bi on Stack Overflow See other posts from Stack Overflow or by Bi
Published on 2010-04-28T23:51:14Z Indexed on 2010/04/28 23:57 UTC
Read the original article Hit count: 182

Filed under:

Hi

I am a C# newbie and have a class that needs to pass row information to a grid in the windows form. What is the best way to do it? I have put in some example code for better understanding. Thanks.

public class GUIController
{   
    private My_Main myWindow;


    public GUIController( My_Main window )
    {
        myWindow = window;
    }

    public void UpdateProducts( List<myProduct> newList )
    {
        object[] row = new object[3];

        foreach (myProduct product in newList)
        {
            row[0] = product.Name;
            row[1] = product.Status;
            row[2] = product.Day;

            //HOW DO I USE BeginInvoke HERE?
       }
    }
}

And the form class below:

public class My_Main : Form
{
    //HOW DO I GO ABOUT USING THIS DELEGATE?
    public delegate void ProductDelegate( string[] row );
    public static My_Main theWindow = null;

    static void Main(  )
    {            
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        theWindow = new My_Main();
        Application.Run(theWindow);

    }

    private void My_Main_Load( object sender, EventArgs e )
    {            

        /// Create GUIController and pass the window object
        gui = new GUIController( this );
     }

    public void PopulateGrid( string[] row )
    {
        ProductsGrid.Rows.Add(row);
        ProductsGrid.Update();

    }

}

Thanks!

© Stack Overflow or respective owner

Related posts about c#