I have a ListView which has checkboxes on the first column. While writing some code for a custom sort (when the user clicks the column header) I’ve come across some weird behavior.
When the user clicks a column header, I’m using a ColumnClickEventHandler to set listView.Sorting to SortOrder.Ascending (or SortOrder.Descending); when this line of code gets hit, it seems to fire off the ItemCheck event handler.
eg
listView.ItemCheck += new ItemCheckEventHandler(listView_ItemCheck);
listView.ColumnClick += new ColumnClickEventHandler(listView_ColumnClick);
...
...
void listView_ColumnClick(object sender, ColumnClickEventArgs e)
{
// Determine whether the column is the same as the last column clicked.
if (e.Column != sortColumn)
{
// Set the sort column to the new column.
sortColumn = e.Column;
// Set the sort order to ascending by default.
listView.Sorting = SortOrder.Ascending;
}
else
{
// Determine what the last sort order was and change it.
if (listView.Sorting == SortOrder.Ascending)
listView.Sorting = SortOrder.Descending;
else
listView.Sorting = SortOrder.Ascending;
}
}
After the line listView.Sorting = SortOrder.Descending; is hit the event handler for listView.ItemCheck is called.
this is probably a bad trick, but you could try :
or
use a boolean property (
private bool disableItemCheck_) used inlistView_ColumnClick(same place as my modifications) and