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.
Share
There are basically 3 ways to display data in a
DataGridViewCreate 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 theCellValueNeededevent to provide the required data to the DGVUse databinding: that’s by far the easiest way. You just fill a
DataTablewith the data from the database using aDbDataAdapter, and you assign thisDataTableto the DGV’sDataSourceproperty. The DGV can automatically create the columns (AutoGenerateColumns = true), or you can create them manually (you must set theDataPropertyNameof 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