Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's better to use: a DataGrid or ListView for displaying large amounts of data?

I want to display >50000 rows in a table. Which is the best control to use: a DataGrid or a ListView (in details view)? Which of these controls will have the better performance?

like image 956
alex dee Avatar asked May 22 '11 17:05

alex dee


People also ask

Which is better ListView or DataGridView?

If you want simple data display then a ListView may be better. If you want data-binding and complex functionality using a DataGridView is better. The Windows Forms ListView control displays a list of items with icons. You can use a list view to create a user interface like the right pane of Windows Explorer.

What is the purpose of using DataGrid control?

A common use of the DataGrid control is to display a single table of data from a dataset. However, the control can also be used to display multiple tables, including related tables. The display of the grid is adjusted automatically according to the data source.

What is the difference between DataGrid and DataGridView?

The DataGrid control is limited to displaying data from an external data source. The DataGridView control, however, can display unbound data stored in the control, data from a bound data source, or bound and unbound data together.

What is the difference between DataGrid and GridView?

The DataGrid and the GridView controls have different event models. The DataGrid control raises single events for operations, while the GridView control is capable of both pre-operation and post-operation events. The GridView control supports the Sorting event that occurs when a field is sorted.


1 Answers

As Hans says in a comment to the original question, they're both going to have abysmal performance, surpassed only by the displeasure that your users will surely experience at the insanity of so many lines of data being displayed at the same time.

But if this is unavoidable in your application (and you provide a very good search function), then you should strongly consider using the virtual mode option, regardless of which control you decide to use. This means that you must provide your own data-management operations, rather than relying on the control to do it for you. The advantage is that things are much faster. As the documentation says:

Virtual mode is designed for use with very large stores of data. When the VirtualMode property is true, you create a DataGridView with a set number of rows and columns and then handle the CellValueNeeded event to populate the cells. Virtual mode requires implementation of an underlying data cache to handle the population, editing, and deletion of DataGridView cells based on actions of the user. For more information about implementing virtual mode, see How to: Implement Virtual Mode in the Windows Forms DataGridView Control.

Or, for the ListView control:

Setting the VirtualMode property to true puts the ListView into virtual mode. In Virtual mode, the normal Items collection is unused. Instead, ListViewItem objects are created dynamically as the ListView requires them.

Virtual mode can be useful under many circumstances. If a ListView object must be populated from a very large collection already in memory, creating a ListViewItem object for each entry can be wasteful. In virtual mode, only the items required are created. In other cases, the values of the ListViewItem objects may need to be recalculated frequently, and doing this for the whole collection would produce unacceptable performance. In virtual mode, only the required items are calculated.

In order to use virtual mode, you must handle the RetrieveVirtualItem event, which is raised every time the ListView requires an item. This event handler should create the ListViewItem object that belongs at the specified index. In addition, the VirtualListSize property must be set to the size of the virtual list.

Handling the SearchForVirtualItem event enables searching in virtual mode. If this event is not handled, the FindItemWithText and FindNearestItem methods will return null.

You can handle the CacheVirtualItems event in order to maintain a cache of ListViewItem objects. If the calculation or lookup to create a ListViewItem object is expensive, maintaining a cache can improve performance.

If the View property is set to Tile, the value will automatically be changed to LargeIcon when VirtualMode is set to true.

In virtual mode, the Items collection is disabled. Attempting to access it results in an InvalidOperationException. The same is true of the CheckedItems collection and the SelectedItems collection. If you want to retrieve the selected or checked items, use the SelectedIndices and CheckedIndices collections instead.

like image 93
Cody Gray Avatar answered Oct 12 '22 04:10

Cody Gray