-
Jan 11th, 2021, 01:48 AM
#1
Thread Starter
Junior Member
Best way to view data from database
Hi all. Lately I have problems when retrieving data from MySQL and show it to users.
There always waiting time to show form.
I'm using ListView to show the data.
I already search in this forum and googling, there are few solutions for slow display ListView:
- Using BeginUpdate and EndUpdate,
- Or using DoubleBuffer by creating class inherit from ListView,
- Or add items to ListView using List of ListViewItem (Array or Collection) (When I use this, there is maximum capacity for collection, so I cannot use this anymore)
I also read that ListView isn't best control to show data.
So what I want to ask is, which control from VB.NET is the best to retrieve and show data from database (regarding from speed)?
Thank you all.
Regards.
-
Jan 11th, 2021, 02:23 AM
#2
Re: Best way to view data from database
The best way is to not use a ListView in the first place. The unique features of the ListView control are the multiple views and grouping. If you are only using Details view and not grouping then using a ListView serves no useful purpose. In the vast majority of cases when displaying tabular data, particularly from a database, you should be using a DataGridView control. Populate a DataTable, using either a MySqlDataAdapter or MySqlDataReader, bind that to a BindingSource and then bind that to your DataGridView. The grid can be read-only if you want to display data only, or you can allow the user edit the data and then save the changes. If you do allow editing, populate the DataTable using a data adapter and then use the same adapter to save the changes back to the database.
If you were going to use a ListView, the issue is that, by default, the control will repaint after you add each item and that is time-consuming, especially when the number of items gets large. The proper way to add items is to create the items first and add them to a List(Of ListViewItem), then call ToArray on that and pass the result to a single call to AddRange. If you don't want to do that for some reason, you can call BeginUpdate first and EndUpdate last to prevent repainting.
-
Jan 11th, 2021, 03:03 AM
#3
Thread Starter
Junior Member
Re: Best way to view data from database
Originally Posted by jmcilhinney
The best way is to not use a ListView in the first place. The unique features of the ListView control are the multiple views and grouping. If you are only using Details view and not grouping then using a ListView serves no useful purpose. In the vast majority of cases when displaying tabular data, particularly from a database, you should be using a DataGridView control. Populate a DataTable, using either a MySqlDataAdapter or MySqlDataReader, bind that to a BindingSource and then bind that to your DataGridView. The grid can be read-only if you want to display data only, or you can allow the user edit the data and then save the changes. If you do allow editing, populate the DataTable using a data adapter and then use the same adapter to save the changes back to the database.
If you were going to use a ListView, the issue is that, by default, the control will repaint after you add each item and that is time-consuming, especially when the number of items gets large. The proper way to add items is to create the items first and add them to a List(Of ListViewItem), then call ToArray on that and pass the result to a single call to AddRange. If you don't want to do that for some reason, you can call BeginUpdate first and EndUpdate last to prevent repainting.
Hello and thank you for your reply.
I have tried using List (Of ListViewItem).
But when total records exceeds 2048, I got error "Collection has reached its maximum capacity".
Is there any other way to use this method?
Or if I can choose other control, do you suggest better to use a DataGridView control instead of any other control?
-
Jan 11th, 2021, 03:25 AM
#4
Re: Best way to view data from database
Originally Posted by weilee
Or if I can choose other control, do you suggest better to use a DataGridView control instead of any other control?
I'm not fond of repeating myself when you can read what I've already posted as often as you like. Read my previous post properly.
-
Jan 11th, 2021, 03:38 AM
#5
Thread Starter
Junior Member
Re: Best way to view data from database
Originally Posted by jmcilhinney
I'm not fond of repeating myself when you can read what I've already posted as often as you like. Read my previous post properly.
Ok. Thank you for your reply.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|