0

I'm adding a great deal of rows to a data grid view, and the process is pretty slow because it appears to try to redraw after each addition.

I'm having trouble finding an example online of how to create a List (or array, whichever works) of rows and add them all at once after the list is created. I need to do this to stop it from re-drawing after each addition though.

Can anyone provide a brief example of this or point me to a good doc?

4

2 に答える 2

1

Here are a couple of ideas:

1) Bind a list to the DataGridView. When you set the DataGridView.DataSource = MyList, it immediately updates the entire grid without all the line-by-line action. You can add the items to MyList and then rebind to the DataGridView. (I prefer using a BindingList which will update the DataGridView grid dynamically.)

2) If data binding is not an option, what I've done in the past is set the AutoSizeMode = NotSet for each column before the update and then set it back to whatever it was before. It is the AutoSizeMode that really slows down the drawing.

于 2012-07-11T23:24:18.053 に答える
1

You're probably looking for the DataGridView.DataSource property. See http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource(v=vs.90).aspx

For example:

//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;

A handy event to bind to at that point is the DataGridView.DataBindingComplete which fires after the data source is bound.

于 2012-07-11T23:24:26.893 に答える