How to sort a listview column that contains file size data? C#

后端 未结 3 859
时光取名叫无心
时光取名叫无心 2021-01-14 20:59

I want to sort the items inside column of ListView, i already made it, but... i can\'t made it with the type of data in column (see picture), someone knows the way for do it

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-14 21:47

    Have you tried what Microsoft has to say for this task? Basically, you'll have to implement a custom comparer, and handle ColumnClick event. But since items in listview are stored as strings, you'll most likely need to do some parsing (assuming you want to sort by size), or implement workaround - sorting datasource and reasign items (which may be easier).

    Quick example of pseudocode for workaround (as implementing custom comparers with parsing from string involved seems too cumbersome for such simple task). In ColumnClick event handler, you could do this:

    // I assume you got list of raw dll sizes somewhere
    dllSizes.Sort();
    List dllSizesForDisplay = new List();
    foreach (var dll in dllSizes)
    {
        dllSizes.Add(new ListViewItem(GetSize(dll.Bytes)));
    }
    
    // reasign items
    listView.Items.Clear();
    listView.Items.AddRange(dllSizesForDisplay);
    

提交回复
热议问题