'ListView'에 해당되는 글 2건

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

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

//리스트뷰 이벤트 중에 칼럼클릭을 추가한다.
 private void lst_emwar_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (m_Columnclick == true)
                lst_emwar.ListViewItemSorter = new ListViewItemComparerASC(e.Column);
            else
                lst_emwar.ListViewItemSorter = new ListViewItemComparerDESC(e.Column); 
            m_Columnclick = !m_Columnclick;
        }
    /// 소스의 제일 마지막에 추가 (또는 클레스 파일로 추가)
    class ListViewItemComparer : IComparer
    {
        private int col;

        public ListViewItemComparer()
        {
            col = 0;
        }

        public ListViewItemComparer(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 ListViewItemComparerASC : IComparer
    {
        private int col;
        public ListViewItemComparerASC()
        {
            col = 0;
        }

        public ListViewItemComparerASC(int column)
        {
            col = column;
        }

        public int Compare(object x, object y)
        {
            try
            {
                // 숫자 비교
                if (Convert.ToDecimal(((ListViewItem)x).SubItems[col].Text) > Convert.ToDecimal(((ListViewItem)y).SubItems[col].Text))
                {
                    return 1;
                }
                else
                    return -1;
            }

            catch (Exception)
            {
                // 텍스트 비교
                return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
            }
        }
    }

    class ListViewItemComparerDESC : IComparer
    {
        private int col;
        public ListViewItemComparerDESC()
        {
            col = 0;
        }

        public ListViewItemComparerDESC(int column)
        {
            col = column;
        }

        public int Compare(object x, object y)
        {
            try
            {
                if (Convert.ToDecimal(((ListViewItem)x).SubItems[col].Text) < Convert.ToDecimal(((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;
            }
        }
    }
Posted by 아르다