2009-02-25 118 views
5

我有一個窗體。它包含幾個datagridviews。在某個時候,用戶可以按下更新數據網格視圖的按鈕。當他們這樣做時,他們通常可以坐下來觀看datagridview,一次一行地重繪。我想爲控件不畫,直到其「完成」,那就是我想要的方式來告訴控制暫停重繪Windows窗體

Control.SuspendRedraw() 
this.doStuff() 
this.doOtherStuff() 
this.doSomeReallyCoolStuff() 
Control.ResumeRedaw() 

我見過的SuspendLayout/ResumeLayout功能,但它們做什麼都沒有(它們似乎更關係到調整大小/移動控件,而不僅僅是編輯它們的數據值?)

+0

可能重複[如何暫停繪畫控制和它的孩子?](http://stackoverflow.com/questions/487661/how-do-i-suspend-painting-for-a-control-and-its-children) – 2011-11-09 17:34:26

+0

請參閱http://stackoverflow.com/問題/ 487661 /怎麼辦,我懸繪畫-FOR-A-CONTRO l和它的孩子 – Simon 2009-03-17 14:47:36

回答

8

有一對夫婦的事情,你可以嘗試:

首先,嘗試在DataGridView的雙緩衝屬性設置爲true。這是實際DataGridView實例上的屬性,而不是表單。這是一個受保護的屬性,因此您必須對網格進行子類別設置。

class CustomDataGridView: DataGridView 
{ 
    public CustomDataGridView() 
    { 
     DoubleBuffered = true; 
    } 
} 

我已經看到了很多的小戰平更新需要對某些視頻卡DataGridView的一段時間,並且這可以通過批處理起來它們是爲顯示被紅牌罰下之前解決你的問題。


另一件事你可以嘗試是Win32消息WM_SETREDRAW

// ... this would be defined in some reasonable location ... 

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] 
static extern IntPtr SendMessage(HandleRef hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam); 

static void EnableRepaint(HandleRef handle, bool enable) 
{ 
    const int WM_SETREDRAW = 0x000B; 
    SendMessage(handle, WM_SETREDRAW, new IntPtr(enable ? 1 : 0), IntPtr.Zero); 
} 

其他地方在你的代碼你有

HandleRef gh = new HandleRef(this.Grid, this.Grid.Handle); 
EnableRepaint(gh, false); 
try 
{ 
    this.doStuff(); 
    this.doOtherStuff(); 
    this.doSomeReallyCoolStuff(); 
} 
finally 
{ 
    EnableRepaint(gh, true); 
    this.Grid.Invalidate(); // we need at least one repaint to happen... 
} 
0

您可以嘗試設置表單以使用DoubleBuffer。 將Form.DoubleBuffer屬性設置爲true,這應該可以解決您的問題。