bug: exception when using ListViewItemSorter
See original GitHub issueHi
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:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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…
Thank you @Amy-Li03