2012-07-11 134 views
0

我向數據網格視圖添加了大量的行,並且該過程非常緩慢,因爲它似乎試圖在每次添加後重新繪製。創建DataGridView行後全部停止刷新

我很難找到一個在線創建列表(或數組,無論哪個工作)的行的例子,並在列表創建後一次添加它們。我需要這樣做,以防止每次添加後重新繪製。

任何人都可以提供一個簡短的例子或指向我一個好的文檔?

+0

http://stackoverflow.com/questions/587508/suspend-redraw-of-windows-form – roymustang86 2012-07-11 15:14:58

+0

是的,謝謝:)我知道我可以暫停控制圖。我只是試圖通過只填充一次控件來解決所有這些問題 - 比如當你建立一個長字符串,然後設置文本框來保存它,而不是在構建文本框時更新文本框100次。 – 2012-07-11 20:52:37

回答

1

您可能正在尋找DataGridView.DataSource屬性。見http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource(v=vs.90).aspx

例如:

//Set up the DataGridView either via code behind or the designer 
DataGridView dgView = new DataGridView(); 

//You should turn off auto generation of columns unless you want all columns of 
//your bound objects to generate columns in the grid 
dgView.AutoGenerateColumns = false; 

//Either in the code behind or via the designer you will need to set up the data 
//binding for the columns using the DataGridViewColumn.DataPropertyName property. 
DataGridViewColumn column = new DataGridViewColumn(); 
column.DataPropertyName = "PropertyName"; //Where you are binding Foo.PropertyName 
dgView.Columns.Add(column); 

//You can bind a List<Foo> as well, but if you later add new Foos to the list 
//reference they won't be updated in the grid. If you use a binding list, they 
//will be. 
BindingList<Foo> listOfFoos = Repository.GetFoos(); 

dgView.DataSource = listOfFoos; 

一個方便的事件結合在該點是在數據源被綁定之後觸發該DataGridView.DataBindingComplete。

+0

感謝您的詳細描述:) – 2012-07-12 17:25:04

1

這裏有幾個想法:

1)綁定列表到DataGridView。當您設置DataGridView.DataSource = MyList時,它會立即更新整個網格,而不會逐行執行任何操作。您可以將這些項目添加到MyList,然後重新綁定到DataGridView。 (我更喜歡使用BindingList來動態更新DataGridView網格。)

2)如果數據綁定不是一個選項,我過去所做的是在更新之前爲每列設置AutoSizeMode = NotSet,然後設置它回到以前的任何事情。這是AutoSizeMode真的減慢了繪圖。