开发者

How to swap two rows in a ListView details view?

What would be the simplest way to swap items in a ListView?

My scenario is to have an arranging method.

sample: I wanted to change the element in the 1st row or index 0 to the 2nd row or index 1

ListView items starting order:

  • |hello|hi|
  • |12345|12|

After swap

How can this be done?


did you try to remove the ListViewItem n from the ListView, keeping a reference to it of course, then you could insert it at the position n-1.

I did not try it out but if I remember well there is ListView.Items.Insert or AddAt which takes an index as parameter and the ListViewItem to add.


well you could create a function that takes two indeces you want to swap and commerce swapping like so:

private: void Swapinlistbox( int indexA, int indexB)
        {
            ListViewItem item = listView1.Items[indexA];
            listView1.Items.Remove(item);
            listView1.Items.Insert(indexB, item);


        }


General Swap method for ListView:

 private void SwapListView(ListView list, ListViewItem itemA, ListViewItem itemB)
 {
        int bIndex = itemB.Index;
        int aIndex = itemA.Index;
        list.Items.Remove(itemB);
        list.Items.Remove(itemA);
        list.Items.Insert(bIndex, itemA);
        list.Items.Insert(aIndex, itemB);
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜