I am building an windows form application, Where i want, when i will open a form like Product, its should load all the listed product available in the database. Along with pics. and shows it up in DataGridView. Now my question is what's the best way to load this much data in a gridview?
-
2Data virtualization. You can also improve performance of the control (see e.g. [this](http://stackoverflow.com/q/4255148/1997232)). But as question stands it's too early to ask it. Have you tried anything? What you aren't happy with? – Sinatr May 08 '17 at 13:10
-
the performance of the grid. when i load this much data it's(gui) getting hanged a lot – Siraj M May 08 '17 at 13:52
1 Answers
If you want to display data in a DataGridView
, and you suspect that there will be a lot of data (possibly so much that it might be problematic to load it into memory completely), you should use its VirtualMode
capabilities. See the MSDN article, "Walkthrough: Implementing Virtual Mode in the Windows Forms DataGridView Control":
"When you want to display very large quantities of tabular data in a DataGridView control, you can set the VirtualMode property to true and explicitly manage the control's interaction with its data store. This lets you fine-tune the performance of the control in this situation."
Basically, virtual mode means that the DataGridView
control will raise the CellValueNeeded
event whenever it needs data. You subscribe a handler to this event and fetch the required data "just-in-time". This makes it possible to keep only a fraction of the full dataset in memory.
It is up to you which and how much data records you keep in memory. Some possible strategies:
You perform paging, i.e. you load a chunk of contiguous rows from the database. This requires a known ordering. That is, you need to be able to derive an
ORDER BY
SQL clause from yourDataGridView
's current sorting. (Check out "Using OFFSET and FETCH to limit the rows returned" to see how paging can be implemented at the SQL level.)You fetch single rows from the database. This might be less efficient than paging, but work better if you cannot determine an
ORDER BY
clause that matches yourDataGridView
's current sort criteria.
In either case, check out the article I linked to above for a tutorial on how to use virtual mode.

- 83,039
- 20
- 168
- 268