Recently write a multithreaded program, need to cross-thread access to DataGridView, bind its datasource, and because the thread is almost every few seconds will be rebinding once DataGridView DataSource, so, encountered a variety of egg pain problems.
First, one of the most common and easiest ways to think about it:
First set System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False on the main thread; Then go to the Designer.cs file and modify the DataGridView declaration to public static and then use dataGridView1 directly in the custom thread. DataSource =dt, then the result is the program inexplicable card dead.
On the internet, everyone says. Cross-thread access control is not scientific to disable cross-thread invocation checking, and it is necessary to declare a delegate to access, so continue tossing and writing the following code
[CSharp]View PlainCopy
- Public delegate void Setdgvsource (DataTable dt);
- public static void Setdgvsourcefunction (DataTable dt)
- {
- if (datagridview1.invokerequired)
- {
- Setdgvsource Delegatesetsource = new Setdgvsource (setdgvsourcefunction);
- Datagridview1.invoke (Delegatesetsource, new object[] {dt});
- }
- Else
- {
- Datagridview1.datasource = DT;
- DATAGRIDVIEW1.COLUMNS[DATAGRIDVIEW1.COLUMNS.COUNT-1]. Visible = false; Set last column not visible
- }
- }
This really can be accessed, at that time, found that when the program ran the minute bar clock appearance, the problem is inexplicably come: inexplicable collapse.
Later continue to check the information, in the forum to see someone said need to add what binding, found not reliable (WinForm program does not need, that is ASP), continue to consult, some people say need to change a property of DataGridView,
[CSharp]View PlainCopy
- Datagridview1.autogeneratecolumns = true;
And then I honestly added, and then the problem was even more wonderful.
The screen of the DataGridView is still a blank, seemingly did not add, just frustrated, suddenly found that the mouse through the DataGridView of the blank area, the mouse from the pointer to the hand type, this is not a description of DataGridView inside there is something!!!!! Sure enough, the mouse in the DataGridView inside the random click Drag, see DataGridView Line just so I drag, it shows up, I drag a line to display a row ....
The egg is extremely painful!!!!
However, in the further debug, I found that if not frequent refresh DataGridView DataSource, visual he is normal ...
Then continue to grope slowly, found that in the case of not changing the binding frequency of the set datasource, if the use of manual binding datasource, he magically good ....
The code is as follows:
[JavaScript]View PlainCopy
- dataGridView1. Rows.clear ();
- foreach (DataRow dr in dt. Rows)
- {
- dataGridView1. Rows.Add (dr["name"], dr["age"]);
- }
That's how you can solve my problem! It's really a joy to cry ...
Perhaps the DataGridView inside of the DataSource directly binding what mechanism caused it can not be updated frequently. You can only use manual methods. Of course, similar to the above method, you can also use the following more concise code
[CSharp]View PlainCopy
- dataGridView1. DataSource =dt. Copy ();
Finally my code is as follows:
[CSharp]View PlainCopy
- Public static void Setdgvsourcefunction (DataTable dt)
- {
- if (datagridview1.invokerequired)
- {
- Setdgvsource Delegatesetsource = new Setdgvsource (setdgvsourcefunction);
- Datagridview1.invoke (Delegatesetsource, new object[] {dt});
- }
- Else
- {
- Datagridview1.datasource = dt. Copy ();
- //datagridview1.rows.clear ();
- //foreach (DataRow dr in Dt. Rows)
- //DATAGRIDVIEW.ROWS.ADD (dr["name"],dr["Age"]);
- Datagridview1.autogeneratecolumns = true;
- DATAGRIDVIEW1.COLUMNS[DATAGRIDVIEW1.COLUMNS.COUNT-1]. Visible = false; Set last column not visible
- }
- }
Then in other files of other threads only need to call this function, you can successfully bind DataGridView datasource!
DataSource that bind DataGridView frequently are not displayed properly