'ListView'에 해당되는 글 2건

  1. 2009.08.07 C# Listview 에서 칼럼헤드 클릭시 정렬 되게 하는.. by 아르다

Listview를 디테일로 해서 칼럼을 만들고 이것저것 하다가 칼럼헤드 클릭시에 정렬이 되도록 할때
사용 하면된다.

001//리스트뷰 이벤트 중에 칼럼클릭을 추가한다.
002 private void lst_emwar_ColumnClick(object sender, ColumnClickEventArgs e)
003        {
004            if (m_Columnclick == true)
005                lst_emwar.ListViewItemSorter = new ListViewItemComparerASC(e.Column);
006            else
007                lst_emwar.ListViewItemSorter = new ListViewItemComparerDESC(e.Column);
008            m_Columnclick = !m_Columnclick;
009        }
010    /// 소스의 제일 마지막에 추가 (또는 클레스 파일로 추가)
011    class ListViewItemComparer : IComparer
012    {
013        private int col;
014 
015        public ListViewItemComparer()
016        {
017            col = 0;
018        }
019 
020        public ListViewItemComparer(int column)
021        {
022            col = column;
023        }
024 
025        public int Compare(object x, object y)
026        {
027            return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
028        }
029 
030    }
031 
032    class ListViewItemComparerASC : IComparer
033    {
034        private int col;
035        public ListViewItemComparerASC()
036        {
037            col = 0;
038        }
039 
040        public ListViewItemComparerASC(int column)
041        {
042            col = column;
043        }
044 
045        public int Compare(object x, object y)
046        {
047            try
048            {
049                // 숫자 비교
050                if (Convert.ToDecimal(((ListViewItem)x).SubItems[col].Text) > Convert.ToDecimal(((ListViewItem)y).SubItems[col].Text))
051                {
052                    return 1;
053                }
054                else
055                    return -1;
056            }
057 
058            catch (Exception)
059            {
060                // 텍스트 비교
061                return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
062            }
063        }
064    }
065 
066    class ListViewItemComparerDESC : IComparer
067    {
068        private int col;
069        public ListViewItemComparerDESC()
070        {
071            col = 0;
072        }
073 
074        public ListViewItemComparerDESC(int column)
075        {
076            col = column;
077        }
078 
079        public int Compare(object x, object y)
080        {
081            try
082            {
083                if (Convert.ToDecimal(((ListViewItem)x).SubItems[col].Text) < Convert.ToDecimal(((ListViewItem)y).SubItems[col].Text))
084                {
085                    return 1;
086                }
087                else
088                    return -1;
089            }
090 
091            catch (Exception)
092            {
093                if (String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text) == 1)
094                {
095                    return -1;
096                }
097                else
098                    return 1;
099            }
100        }
101    }
Posted by 아르다