1

I have to make some modification to an application that have perfomance issues. I ran the profiler and one of the problem is that one:

A sql query is made and stored in a DataReader. The query returns about 2000 rows.

then a while loop start

Dim monSQL as IDataReader 
monSQL = a SQL Query

Do While monSQL.Read
  strArray(0) = 
  strArray(1) = 
  strArray(2) = 
  strArray(3) = monSQL("Something").ToString
  strArray(4) = monSQL("Something").ToString
  strArray(5) = monSQL("Something").ToString

  If Not IsDBNull(monSQL("Something")) Then
    strArray(6) = (monSQL("Something"))
    strArray(7) = monSQL("Something")).ToString
    strArray(8) = monSQL("Something").ToString
    strArray(9) = monSQL("Something").ToString
    strArray(10) = monSQL("Something").ToString
  End If

  objListItem = New ListViewItem(strArray)
  objListItem.Tag = lngNoLot
  ListView.Items.Add(objListItem)
Loop

In the while loop, the data are insert in the listview

It takes quite some time to go throught the loop (about 10 sec)

What are my option to make things go faster?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • If you fill a DataTable you can use that as the Datasource for a DataGridview and save lots of time and code – Ňɏssa Pøngjǣrdenlarp Jun 04 '15 at 17:10
  • Would i still be able to have a check box at the beginning of each row to select a certain row? – Maxime Paré Jun 04 '15 at 17:23
  • [How to speed adding items to a ListView?](http://stackoverflow.com/questions/9008310/how-to-speed-adding-items-to-a-listview) – Steve Jun 04 '15 at 17:27
  • The problem is not in inserting the item. Even if I comment everything in the while loop, i.e not adding anything to the list, it takes the same amount of time. – Maxime Paré Jun 04 '15 at 17:30
  • A ListView is sub-optimal for viewing DB data - you are actually making string copies of the data -and lots of subitems - to add to the LV. – Ňɏssa Pøngjǣrdenlarp Jun 04 '15 at 17:35
  • Wait a minute. Are you claiming it takes 10 seconds just to loop through a 2,000 record reader and nothing else? Something "else" is going on. – LarsTech Jun 04 '15 at 17:35
  • yes, if I comment out everything in the while loop, it takes 00:00:08.1171575 seconds to go trought the loop. And about 8.5 second to load the window that show the empty listview. If I comment out the function that do the filling of the listview, It take about 0.1 second to open the empty listview. The SQL query takes about 0,03 second. – Maxime Paré Jun 04 '15 at 17:41

1 Answers1

1

Try suspending any drawing during the loop updates:

listView1.BeginUpdate()
// your loop
listView1.EndUpdate()
LarsTech
  • 80,625
  • 14
  • 153
  • 225