ListView 아이템 이동하기

ListView 아이템을 마우스로 끌어서 이동하는 예제입니다.
한 줄 전체를 그리고 여러 셀을 선택하여 이동할수 있습니다.

전체 소스
WATListViewMove.zip


     

소스

 private void listView1_MouseMove(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                this.Cursor = Cursors.SizeAll;
            }
        }

        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {
            bool bSamePosition = false;
            this.Cursor = Cursors.Arrow;
            ListViewItem selected = this.listView1.GetItemAt(e.X, e.Y);
            if(null != selected)
            {
                foreach (ListViewItem l in listView1.SelectedItems)
                {
                    if (l.Index == selected.Index)
                    {
                        bSamePosition = true;
                        break;
                    }
                }
                if(!bSamePosition)
                {
                    foreach (ListViewItem l in listView1.SelectedItems)
                    {

                        l.Remove();
                        listView1.Items.Insert(selected.Index, l);
                   }
                }
            }
        }

       


Leave a Comment