I have a data structure(bindingList)that receives data from a another thread with I’ve bound to a dataGridView which is throwing a cross thread exception. how do I invoke a dataGridView which is dataBound? This is a winForm project. Here’s an example of what I’m talking about for clarity:
DataStore dStore = new DataStore();
dStore.ReceiveData += new ReceiveDataEventHndlr(data);
BindingList<mydataobj> myDataStructure = new BindingList<mydataobj>();
dataGridView.DataSource = myDataStructure;
// here's my cross threading issue
private void data(string s, double d)
{
myDataStructure.Add(new MyDataObj(s,d));
}
You need to use
Control.Invokewhen modifying Controls from another thread:First, you check to see if
Control.InvokeRequired. If so, then callInvoke()with a delegate to the same function, and then return. It will re-enter the function from the GUI thread,InvokeRequiredwill befalse, and the control will be updated.