ListView의 column을 클릭했을 때 리스트를 정렬하는 예제입니다.
http://whiteat.com/zbxe/33283 예제를 이용하겠습니다.
전체 소스
주요 소스
Boolean m_ColumnclickASC = true;
private void lvwColor_ColumnClick(object sender, ColumnClickEventArgs e)
{
if (m_ColumnclickASC == true)
((ListView)sender).ListViewItemSorter = new ListViewItemSortASC(e.Column);
else
((ListView)sender).ListViewItemSorter = new ListViewItemSortDESC(e.Column);
m_ColumnclickASC = !m_ColumnclickASC;
}
class ListViewItemSort : IComparer
{
private int col;
public ListViewItemSort()
{
col = 0;
}
public ListViewItemSort(int column)
{
col = column;
}
public int Compare(object x, object y)
{
return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
}
}
class ListViewItemSortASC : IComparer
{
private int col;
public ListViewItemSortASC()
{
col = 0;
}
public ListViewItemSortASC(int column)
{
col = column;
}
public int Compare(object x, object y)
{
try
{
if (Convert.ToInt32(((ListViewItem)x).SubItems[col].Text) > Convert.ToInt32(((ListViewItem)y).SubItems[col].Text))
return 1;
else
return -1;
}
catch (Exception)
{
if (1 != String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text) )
return -1;
else
return 1;
}
}
}
class ListViewItemSortDESC : IComparer
{
private int col;
public ListViewItemSortDESC()
{
col = 0;
}
public ListViewItemSortDESC(int column)
{
col = column;
}
public int Compare(object x, object y)
{
try
{
if (Convert.ToInt32(((ListViewItem)x).SubItems[col].Text) < Convert.ToInt32(((ListViewItem)y).SubItems[col].Text))
return 1;
else
return -1;
}
catch (Exception)
{
if (String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text) == 1)
return -1;
else
return 1;
}
}
}
- [2018/02/26] List 에서 고유값 얻기 ()
- [2015/05/22] C#, 아두이노 간의 WIFI 통신으로 LCD 제어 (4566)
- [2015/03/13] 항상 마지막에 추가한 TEXT 보이게 ()
- [2014/01/17] ComboBox Text 편집 안되게 (14491)
- [2014/01/08] if 문에서 여러개 비교할때 (25340) *3
- [2013/12/30] C++, C# 간단한 기능 비교 (12884)
- [2013/12/18] 3자리마다 ,(콤마) 찍기 (원화, 달러 표시) (15224)
- [2013/09/29] 설치된 IE 버전 얻기 (12397)
- [2013/08/29] byte array to Hexa String (13026)
- [2013/06/25] string array to string (스트링 문자열 합치기) (23713)