.net Compact Framework으로 프로그램 개발 시 ListView를 이용하는 경우가 많습니다.
가끔 고객 요구 사항 중 컬럼 클릭 했을 경우 Sorting 기능을 요구할때 사용할 만한 예제이오니 참고해보세요...
1. 클래스를 생성
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;
public class SortWrapper
{
internal ListViewItem sortItem;
internal int sortColumn;
// A SortWrapper requires the item and the index of the clicked column.
public SortWrapper(ListViewItem Item, int iColumn)
{
sortItem = Item;
sortColumn = iColumn;
}
// Text property for getting the text of an item.
public string Text
{
get
{
return sortItem.SubItems[sortColumn].Text;
}
}
// Implementation of the IComparer
// interface for sorting ArrayList items.
public class SortComparer : IComparer
{
bool ascending;
// Constructor requires the sort order;
// true if ascending, otherwise descending.
public SortComparer(bool asc)
{
this.ascending = asc;
}
// Implemnentation of the IComparer:Compare
// method for comparing two objects.
public int Compare(object x, object y)
{
SortWrapper xItem = (SortWrapper)x;
SortWrapper yItem = (SortWrapper)y;
string xText = xItem.sortItem.SubItems[xItem.sortColumn].Text;
string yText = yItem.sortItem.SubItems[yItem.sortColumn].Text;
return xText.CompareTo(yText) * (this.ascending ? 1 : -1);
}
}
}
public class ColHeader : ColumnHeader
{
public bool ascending;
public ColHeader(string text, int width, HorizontalAlignment align, bool asc)
{
this.Text = text;
this.Width = width;
this.TextAlign = align;
this.ascending = asc;
}
}
2. 소스부분-컬럼 추가
public Form1()
{
InitializeComponent();
//ListView Column Add
listView1.Columns.Add(new ColHeader("자산번호", 110, HorizontalAlignment.Left, true));
listView1.Columns.Add(new ColHeader("자산명", 90, HorizontalAlignment.Left, true));
listView1.Columns.Add(new ColHeader("구분", 60, HorizontalAlignment.Center, true));
listView1.Columns.Add(new ColHeader("상태", 60, HorizontalAlignment.Center, true));
}
3. 소스-구현부(listview의 ColumnClick 이벤트 추가 후 작업)
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
try
{
ColHeader clickedCol = (ColHeader)this.listView1.Columns[e.Column];
clickedCol.ascending = !clickedCol.ascending;
int numItems = this.listView1.Items.Count;
this.listView1.BeginUpdate();
ArrayList SortArray = new ArrayList();
for (int i = 0; i < numItems; i++)
{
SortArray.Add(new SortWrapper(this.listView1.Items[i], e.Column));
}
SortArray.Sort(0, SortArray.Count, new SortWrapper.SortComparer(clickedCol.ascending));
// Clear the list, and repopulate with the sorted items.
this.listView1.Items.Clear();
for (int i = 0; i < numItems; i++)
this.listView1.Items.Add(((SortWrapper)SortArray[i]).sortItem);
SortArray.Clear();
// Turn display back on.
this.listView1.EndUpdate();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
'C#' 카테고리의 다른 글
[C#]File to Byte Array Convert(Byte 형변환) 소스 코드 (0) | 2016.01.19 |
---|---|
[C#]Socket을 이용한 바코드 프린터 출력 예제 소스 (0) | 2016.01.19 |
[C#]바코드 생성 프로그램(Barcode Generator) (0) | 2016.01.18 |
PDA 화면 캡쳐 유틸프로그램 (0) | 2016.01.18 |
[C#/PDA]MessageBox Auto Closing (0) | 2016.01.18 |