Question

I want to sort my listview by columns and I used this example code: http://support.microsoft.com/kb/319401. It works but at the end of listview I have some blank items and when I click a column these items become first items and I need them at the end. How can I make the code to skip the blank items?

Thanks.

Was it helpful?

Solution

If you have implemented the whole example you can change this part of the Compare method:

// Compare the two items
compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text,
                                      listviewY.SubItems[ColumnToSort].Text);

to something like:

// Compare the two items
compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text,
                                      listviewY.SubItems[ColumnToSort].Text);
if (listviewX.SubItems[ColumnToSort].Text=="") 
        compareResult = (OrderOfSort == SortOrder.Descending ? -1:  1);
else if (listviewY.SubItems[ColumnToSort].Text=="") 
       compareResult = (OrderOfSort == SortOrder.Descending ? 1 : -1);

This should override the original result of the comparison.

Watch how you check for empty items, could be null, too or whatever you did before..!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top