Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to fill DataGridView with large amount of data

Tags:

I have a windows form that has two DataGridViews (DGVs) that will hold 25,000+ records and 21 columns each. I have successfully loaded each with the data from the DB using a DataAdapter and then I tried simply filling the DGVs using for loops. Each method took roughly the same amount of time. The first time the data is filled into the DGVs it takes too long (7+ mins), and then the subsequent times the time is much more reasonable (~30 secs). So my question is, what is the best way to load a DGV with a large amount of data that will take on average <= 1 min? I really like the functionality of DGVs, but if push comes to shove I am willing to use a different technology, even if it means giving up some of that functionality.

like image 364
Bkins Avatar asked Aug 26 '10 23:08

Bkins


4 Answers

There are basically 3 ways to display data in a DataGridView

  • Create the rows manually in a loop, as you are currently doing: as you have noticed, it's very inefficient if you have a lot of data

  • Use the DataGridView's virtual mode, as suggested by Jonathan in his comment: the DGV only creates as many rows as can be displayed, and dynamically changes their contents when the user scrolls. You need to handle the CellValueNeeded event to provide the required data to the DGV

  • Use databinding: that's by far the easiest way. You just fill a DataTable with the data from the database using a DbDataAdapter, and you assign this DataTable to the DGV's DataSource property. The DGV can automatically create the columns (AutoGenerateColumns = true), or you can create them manually (you must set the DataPropertyName of the column to the name of the field you want to display). In databound mode, the DGV works like in virtual mode except that it takes care of fetching the data from the datasource, so you don't have anything to do. It's very efficient even for a large number of rows

like image 129
Thomas Levesque Avatar answered Sep 28 '22 01:09

Thomas Levesque


I think you can use DataReader method instead of DataAdapter. DataReader is very efficient oneway component, because it's only reading data from the source, and you can fill a data table with looping.

like image 34
Nanda Avatar answered Sep 28 '22 00:09

Nanda


If you have a huge amount of rows, like 10 000 and more,

to avoid performance leak - do the following before data binding:

dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing; 
//or even better .DisableResizing. 
//Most time consumption enum is DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders
dataGridView1.RowHeadersVisible = false; // set it to false if not needed

after data binding you may enable it.

like image 44
okarpov Avatar answered Sep 28 '22 00:09

okarpov


Try to use a DataTable. Fill it. Then use a DataView. Assign it to the DataGridView DataSource.

//DataView dataView = new DataView(dataTable);
//this.Grid.DataSource = dataView;

You will get very SMALL response times for big files (25000 records and 21 columns in a sec). My template program took 7 sec to load 100 000 Rows * 100 Columns {with stupid contents -> row number as string}

like image 41
C. Lorphelin Avatar answered Sep 28 '22 01:09

C. Lorphelin