In a piece of code that binds a able object to the DataGrid Control,
After a thread is added, the system prompts "The control created on a thread cannot be the parent level of the Control created on another thread"
In the csdn Forum, find other people's suggestion. "Use the delegate method to obtain control of the control first, and then perform operations on the control"
The idea is implemented.
Step 1: declare delegation and instance-based Delegation
Private delegate sub binddatagriddelegate () 'create a delegate and delegate object
Private mybinddatagriddelegate as binddatagriddelegate = new binddatagriddelegate (addressof selectdata) 'selectdata is the code segment containing the data binding of the DataGrid
Step 2: Prepare to join the thread
Private bindgridthread as thread
Private sub invokebinddatagrid () '// you must use invoke or begininvoke to call the operations of controls created by this thread. Otherwise, an exception is thrown.
Dg_ysinfo.invoke (mybinddatagriddelegate, nothing)
End sub
Private sub stopbindthread ()
If not bindgridthread is nothing then
If bindgridthread. isalive then
Bindgridthread. Abort ()
Bindgridthread. Join ()
End if
End if
Bindgridthread = nothing
End sub
Step 3: In the button clicking event, call the thread to bind the DataGrid to the query result.
Private sub btrun_click (byval sender as system. Object, byval e as system. eventargs) handles btrun. Click
Stopbindthread ()
Bindgridthread = new thread (New threadstart (addressof invokebinddatagrid) '; // The call is valid by Delegate.
Bindgridthread. Start ()
End sub
After completing the preceding three steps, we finally solved the problem of "The control created on a thread cannot be the parent level of the Control created on another thread,
The test results are expected