question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug: exception when using ListViewItemSorter

See original GitHub issue

Hi

Following code throws an exception, while it works fine in .NET 4.8 I use .NET 5.0 latest SDK and latest VS 2019 community. I’m using a WinForms application .NET 5.0

WHEN: The exception is thrown when you click the same column header the THIRD time (first and second time it works and no exception thrown).

WHAT: Exception thrown: ‘System.NullReferenceException’ in System.Windows.Forms.dll Object reference not set to an instance of an object.

WHERE: Exception thrown on code line: listViewCardSets.ListViewItemSorter = new ListViewItemStringComparer(e.Column, listViewCardSets.Sorting);

class ListViewItemStringComparer : IComparer
    {
        private int col;
        private SortOrder order;
        public ListViewItemStringComparer()
        {
            col = 0;
            order = SortOrder.Ascending;
        }

        public ListViewItemStringComparer(int column, SortOrder order)
        {
            col = column;
            this.order = order;
        }

        public int Compare(object x, object y)
        {
            int returnVal = -1;
            returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
                                       ((ListViewItem)y).SubItems[col].Text);

            // Determine whether the sort order is descending.
            if (order == SortOrder.Descending)
                // Invert the value returned by String.Compare.
                returnVal *= -1;

            return returnVal;
        }
    }
        private int _sortColumnIndex = -1;
        private void listViewCardSets_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            // Determine whether the column is the same as the last column clicked.
            if (e.Column != _sortColumnIndex)
            {
                // Set the sort column to the new column.
                _sortColumnIndex = e.Column;
                // Set the sort order to ascending by default.
                listViewCardSets.Sorting = SortOrder.Ascending;
            }
            else
            {
                // Determine what the last sort order was and change it.
                if (listViewCardSets.Sorting == SortOrder.Ascending)
                    listViewCardSets.Sorting = SortOrder.Descending;
                else
                    listViewCardSets.Sorting = SortOrder.Ascending;
            }

            // Call the sort method to manually sort.
            listViewCardSets.Sort();

            // Set the ListViewItemSorter property to a new ListViewItemComparer object.
            listViewCardSets.ListViewItemSorter = new ListViewItemStringComparer(e.Column, listViewCardSets.Sorting);
        }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Nov 30, 2020

Make an empty win forms app for .Net 5.0 sdk, add a ListView with name as in the code. Assign the column_click method to it.

Don’t they learn you this at Microsoft?

I now understand where all those bugs come from…

0reactions
RussKiecommented, Nov 30, 2020

Thank you @Amy-Li03

Read more comments on GitHub >

github_iconTop Results From Across the Web

windows forms: listview column click sort #14749 - dotnet/sdk
hi In .net 4 I implemented listview column click sorting by implementing a listviewitemsorter class and use this for the listview.
Read more >
c# - ListView strange exception
This code works fine with the first device, but it throws an exception at Items.Add with the second one. The exception does not...
Read more >
ListView.Items.Add throws ExecutionEngineException with ...
Problem description: ListView.Items.Add throws ExecutionEngineException upon reaching a certain number of items in the ListView when ListView.
Read more >
Thread: Problem sorting list view [UnResolved]
My sorting function for my list view will sort a column. There is no problem with that. The problem comes up when I...
Read more >
Error while changing the selected item in listview. Help!
The event fires for selected item change, when a new item is selected the old selected item deselects first and this fires the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found