Yesterday I found a "strange" behavior in .NET WinForm project I’m developing. When the application started the main window appeared in 4 seconds. When I pressed refresh button the same method run in 24 seconds! First when I didn’t notice this fact, I thought the problem is about the SQL query. I added an "order by" statement two days ago to the original query. When I’ve tried the query in SQLite Administrator this query only took 0,324 miliseconds to run.

In fact I used a function which makes possible to change the order of the column in the listview. When the application starts the ListView is empty. Then I call a function which clears the Column collection of the Listview, but doesn’t clear the items of the Listview. ListViewItems was cleared after the columns were added. It was the problem! When the items was not cleared it took a lot of time (24 sec) to add these 10 columns to the Columns collection.

Conclusion: Always call ListView.Clear() method instead of ListView.Columns.Clear() on a non empty ListView before manipulating the columns of this control. I didn’t realized that slowdown before because I didn’t display so much data in a ListView (>4000 records). I thing RealizeAllSubItems() method call makes it so slow which is invoked in InsertColumn(int index, ColumnHeader ch, bool refreshSubItems).