Results 1 to 5 of 5

Thread: Best way to view data from database

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Question 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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Best way to view data from database

    Quote Originally Posted by jmcilhinney View Post
    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?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Best way to view data from database

    Quote Originally Posted by weilee View Post
    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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    18

    Re: Best way to view data from database

    Quote Originally Posted by jmcilhinney View Post
    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
  •  



Click Here to Expand Forum to Full Width