views:

77

answers:

0

Say I have the following class/Form (semi psuedo):

public partial class X : Form
{
    private DataTable dt;
    private BackgroundWorker bg;

    public X()
    {
        dt.Columns.Add("A");
        dt.Columns.Add("B");
        dt.Columns.Add("C"); 
    }

    private void button_Click(...)
    {
        bg = new BackgroundWorker();  
        bg.DoWork += new DoWorkEventHandler(bg_DoWork);
        bg.RunWorkerAsync();
    }

    private void bg_DoWork(...)
    {
        // do some stuff  
        MagicMethod(parameters);

        // doesnt work, how to fix this?
        dataGridView1.Invoke((Action)delegate { dataGridView1.DataSource = dt; });
    }

    private void MagicMethod(params)
    {
        // update a label, WORKS 
        label1.Invoke((Action) delegate { label1.Text = "somestuff" }

        // do some stuff to fill the datatable
    } 
}

Ofcourse this is a distilled version, without the BackgroundWorker everything is sound, but since I want a more responsive UI I try to implement one. But the grid doesnt get updated by the worker (whereas the label does correctly). Any tips?