简体   繁体   中英

Why does changing the HeaderStyle of a ListView hang the application?

I've been trying to fix this issue and it seems to be a problem from the .Net framework. anyway, I have a listview which contains 5000+ items and a button used to do further processing to the selected items in the listview. now clicking this button should also set "listview.HeaderStyle" property of the listview to "ColumnHeaderStyle.Nonclickable". now when I do that. the program hangs for like 10 seconds then continues its work. I have no idea what is causing and why this is happening. I hope you guys have a solution for this. any ideas?

I've found a basic solution for now, all I need is to set

myListview.ColumnClick += new ColumnClickEventHandler(delegate{});

Now I don't need to change "listview.HeaderStyle" property anymore. basically I was disabling the ColumnClick event from the HeaderStyle property and that's all. so instead of setting the HeaderStyle to nonClickable. I just remove/change the function inside event handler.

You can solve this problem using BackgroundWorker MSDN .

Note : Even using this solution your form will hang for some time. Because you have 5000+ items to be bind to the list, which will block your UI thread , hence winform will hang. But your listview.HeaderStyle will get modified.

Try this

  1. On button click, you directly change the propert of listview.HeaderStyle to ColumnHeaderStyle.Nonclickable . Then call RunWorkerAsync of BackgroundWorker .
  2. On DoWork event handler you do the processing, and once done bind data to list view. To do this you will need to add following extension class to your project.

Extension Class

public static class ControlExtensions
{
    public static void Invoke(this Control control, Action action)
    {
        if (control.InvokeRequired) control.Invoke(new MethodInvoker(action), null);
        else action.Invoke();
    }
}

Using this you can bind data to listview

listview.Invoke(() => ( listview.DataSource = dataSource; });

Hope this works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM