2012-10-24 32 views
2

一個一般性的問題有關實施意見一個DataGridView虛擬模式。實現了一個數據綁定

我有綁定到一個datagridview集合。

BindingList<Line> allLines = new BindingList<Line>(); 
dataGridView1.DataSource = allLines; 

我想要實現virtual mode因爲集合可能包含數百萬個條目(Line對象),所以我認爲它可能是更快地只是「緩存」或顯示需要在同一時間幾個條目。我理解虛擬模式是爲了什麼?

我看:http://msdn.microsoft.com/en-us/library/2b177d6d.aspx

但我不能讓它的datagridviewdatabound工作。

我不能指定行數:

this.dataGridView1.RowCount = 20; 
`RowCount property cannot be set on a data-bound DataGridView control.` 

編輯:這個鏈接提示我可以有完全去除結合。是這樣嗎? http://msdn.microsoft.com/en-us/library/ms171622.aspx

'如果綁定模式不符合您的性能需求,您可以通過虛擬模式事件處理程序管理自定義緩存中的所有數據。

回答

3

如果你想使用DataGridView.VirtualMode,那麼你就不能指望用綁定數據集。他們atr相反。因此,您不需要設置DataSource,而只需設置RowCount屬性併爲DataGridView.CellValueNeeded Event提供事件處理程序。

另外你需要先設置dataGridView.VirtualMode屬性爲true,可能寫在設計器裏。默認情況下,它設置爲false,這就是爲什麼你會得到一個異常,並說你不能設置RowCount

可能是你必須手動初始化網格列。

雖然刷新了您的網格(比如按鈕點擊),你必須

dataGridView.RowCount = 0; 
\\refresh your cache, where you store rows for the grid 
\\... 
dataGridView.RowCount = yourCache.Count;//or some other property, getting the number of cached rows. 

CellValueNeeded事件將被解僱的每一行每一列,根據不同的行數和列數。您需要根據e.RowIndexe.ColumnIndex設置e.Value與事件處理程序中處理的單元格的值。

所以,這個工作你需要至少處理CellValueNeeded。如果您的DataGridView是隻讀的,則其他事件不是必需的。

更完整和連續的概述,請Virtual Mode in the Windows Forms DataGridView Control

+0

好的,謝謝你的回覆。我認爲他們互相補充。但例外情況是自我解釋,虛擬模式已被設置爲true。 – Jimmy

相關問題