Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView ScrollBars are not working after Thread

I'm setting a dataTable as data source of a datagridview. I do this on a new thread (I don't want my UI blocked while is loading data). My dilema is: the scrollbars are not working after the thread finishes. I tryed setting Scrollbars.None before load the data, and Scrollbars.Both after. Also tryed with Refresh. But only helped me to show the scrollbars, these still without working.

If I use the same code in my principal, thread it works perfectly.

so how can I do to make it work?

My code:

private void PressKey(object sender, KeyPressEventArgs e)
    {
        var process = new Thread(this.LoadData);
        process.Start();
    }

private void LoadData()
    {
        CheckForIllegalCrossThreadCalls = false;
        this.dgv.ScrollBars = ScrollBars.None;
        this.dgv.Columns.Clear();
        this.dgv.DataSource = MyDataTable;
        this.dgv.ScrollBars = ScrollBars.Both;
    }
like image 520
DannSaHa Avatar asked Mar 15 '23 12:03

DannSaHa


1 Answers

Ok, I finally got it. I used MethodInvoker inside of my thread. It allows me run on UI thread and update the controls:

private void LoadData()
{
    CheckForIllegalCrossThreadCalls = false;
    this.dgv.Columns.Clear();
    this.dgv.DataSource = MyDataTable;

    this.Invoke((MethodInvoker)delegate
    {
        dgv.ScrollBars = ScrollBars.Both; // runs on UI thread
    });

}
like image 85
DannSaHa Avatar answered Mar 18 '23 04:03

DannSaHa