Files
VAR.DatabaseExplorer/ServerExplorer/Controls/CustomListView.cs

88 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Collections;
namespace ServerExplorer.Controls
{
[DefaultProperty("Items")]
[DefaultEvent("SelectedIndexChanged")]
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[Docking(DockingBehavior.Ask)]
public class CustomListView : ListView
{
#region Declarations
ListViewItemComparer itemComparer = new ListViewItemComparer();
#endregion
#region Properties
private bool allowSorting = false;
public bool AllowSorting
{
get { return allowSorting; }
set { allowSorting = value; }
}
#endregion
#region Control life cicle
public CustomListView()
{
ColumnClick += CustomListView_ColumnClick;
}
#endregion
#region Events
private void CustomListView_ColumnClick(object sender, ColumnClickEventArgs e)
{
if (!allowSorting) { return; }
itemComparer.SetColumn(e.Column);
ListViewItemSorter = itemComparer;
Sort();
}
#endregion
}
#region ListViewItemComparer
internal class ListViewItemComparer : IComparer
{
private int _col;
private bool _ascending;
public void SetColumn(int col)
{
_col = col;
_ascending = _col != col || !_ascending;
}
public int Compare(object x, object y)
{
var itemA = (ListViewItem) x;
var itemB = (ListViewItem) y;
if (itemA.SubItems.Count <= _col || itemB.SubItems.Count <= _col)
{
return -1;
}
return _ascending
? String.CompareOrdinal(itemA.SubItems[_col].Text, itemB.SubItems[_col].Text)
: String.CompareOrdinal(itemB.SubItems[_col].Text, itemA.SubItems[_col].Text);
}
}
#endregion
}