Search code examples
c#objectlistview

rebuilding columns and adding a new dataset to DataListView


So I am using the DataListView variant from BrightIdeasSoftware for my c# project.

I use buttons to change the view of the DataListView. When a button is pressed the following is executed:

olv.DataSource = null;

olv.AllColumns = colList;
olv.RebuildColumns();

//Fill OLV with data
olv.DataSource = dt;

The first button creates 1 column and changes the view to View.Tile;.

The second button creates 4 columns and changes the view to View.Details;.

The new columns are shown immediately but it takes about a second for the data to show up in the list. It takes even longer when I rebuild a larger amount of columns.

When I run my application it builds the view with 4 columns in View.Details instantly. Only when I switch from the first button view to the second button view it hangs for a moment.

In debug mode I noticed that RebuildColumns() is the one that hangs. But if I leave olv.DataSource = dt; out, code after RebuildColumns() is executed immediatly.

Can someone explain to me why this is happening?

Thanks


Solution

  • First of all it is not clear to me if the lists that you are switching between use the same DataTable (I assume that's the type of your dt object). If source is identical then you needn't add and remove columns, you can set OLVColumn.IsVisible. That's faster.

    Second, setting ObjectListView.DataSource = null won't remove items from the list (you need ObjectListView.ClearObjects for that), but what that will do is invalidate the internal DataSourceAdapter.CurrencyManager which will block any item updates until ObjectListView.DataSource is specifically set again. Setting the data source will add items to your list (calls ObjectListView.BuildList) which might be expensive if your source is large.

    In conclusion:

    If you just switch between lists with shared datasource then:

    foreach (var column in this.dataListView.AllColumns)
        column.IsVisible = true;
    // call this only when tampering with columns
    this.dataListView.RebuildColumns();
    // if you need to add/remove items, same philosophy, partial instead of 
    // complete update use filtering instead of DataSource reset
    

    If lists do not have a common datasource and therefore columns totally differ, then it is a price you have to pay, rebuilding columns and items, but you could escape with a TabControl for instance. You could switch between lists without having to reset DataSource all the time. That should be a one time experience.