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;
}
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
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With