2015-05-20 85 views
0

我已经创造了一个C#DataGridView的DataGridView不刷新立即

private void SomeFunc() 
{ 
    DataGridView errorDataGrid = new System.Windows.Forms.DataGridView(); 

    // Set properties for DataGrid View 
    // .. 
    // .. 

    // Add to the panel 
    somePanel.Controls.Add(this.errorDataGrid); 
} 

private void ShowOnGrid(string _message) 
{ 
    // create a new row and add a text to it 
    DataGridViewRow newRow = new DataGridViewRow(); 
    newRow.CreateCells(this.errorDataGrid, _message); 

    // add this row to grid 
    this.errorDataGrid.Rows.Add(newRow); 
    // and scroll the bar to the newly added row, [currentRow = 0 initially] 
    this.errorDataGrid.FirstDisplayedScrollingRowIndex = currentRow++; 
} 

问题是数据(_message)在添加到网格后不会立即显示。相反,它突然显示,在节目结束

ShowOnGrid("My Message to show - 1") 
ShowOnGrid("My Message to show - 2") 
ShowOnGrid("My Message to show - 3") 
ShowOnGrid("My Message to show - 4") 
ShowOnGrid("My Message to show - 5") 
ShowOnGrid("My Message to show - 6") 
// End of program 

所以所有的消息都显示在我的程序结束。 我错过了什么?或者在上面的程序中出现错误?


我提到以下,但找不到任何显著:

回答

0

ShowOnGrid方法被调用的UI线。所以,当调用方法时,例如CallShowOnGrid被调用,然后调用ShowOnGrid方法,则UI线程被阻塞。一旦所有ShowOnGrid方法执行完毕,它只会被刷新(称之为)。

如果你能证明它调用ShowOnGrid方法,多一些情境的方法,有人可以提供解决方案。

+0

嘿,我通过在网格中添加一行后将其更正。我加了'dataGrid.Refresh()' – SimpleGuy