I need the code to reorder the items in a ListView. I want to select an item and click an Up or Down Button and move the selected ListView Item.
This article will show you all of the code to move a ListView Item up or down. The code is reusable for any ListView because the ListView object is passed by reference to the reordering method, along with a parameter that tells the method whether to move the Item up or down.
To use the code for this article, you will need to create a Winows Form in Visual Studio .NET. Place a ListView on the form, along with two command buttons for moving the items up or down. Figure 1 shows a form with those and other controls.
Figure 1 - Test Form for Moving ListView Items.
Figure 2 has the code for the up and down button event handlers. They call the MoveListViewItem method.
Figure 2 - Up and Down Button Handlers.
| Private Sub btnUp_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnUp.Click Me.MoveListViewItem(Me.lvRegions, moveUP:=True) End Sub Private Sub btnDown_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnDown.Click Me.MoveListViewItem(Me.lvRegions, moveUP:=False) End Sub |
| ''' <summary> ''' Move selected listview item up or down based on moveUp= True/false. ''' </summary> ''' <param name = "moveUp"></param> Private Sub MoveListViewItem(ByRef lv As ListView, ByVal moveUp As Boolean) Dim i As Integer Dim cache As String Dim selIdx As Integer With lv selIdx = .SelectedItems.Item(0).Index If moveUp Then ' ignore moveup of row(0) If selIdx = 0 Then Exit Sub End If ' move the subitems for the previous row ' to cache so we can move the selected row up For i = 0 To Items(selIdx).SubItems.Count - 1 cache = .Items(selIdx - 1).SubItems(i).Text .Items(selIdx - 1).SubItems(i).Text = _ .Items(selIdx).SubItems(i).Text .Items(selIdx).SubItems(i).Text = cache Next .Items(selIdx - 1).Selected = True .Refresh() .Focus() Else ' ignore move down of last row If selIdx = .Items.Count - 1 Then Exit Sub End If ' move the subitems for the next row ' to cache so we can move the selected row down For i = 0 To Items(selIdx).SubItems.Count - 1 cache = .Items(selIdx + 1).SubItems(i).Text .Items(selIdx + 1).SubItems(i).Text = _ .Items(selIdx).SubItems(i).Text .Items(selIdx).SubItems(i).Text = cache Next .Items(selIdx + 1).Selected = True .Refresh() .Focus() End If End With End Sub |
| private void MoveListViewItem(ref ListView lv,bool moveUp) { string cache; int selIdx; selIdx = lv.SelectedItems[0].Index; if(moveUp) { // ignore moveup of row(0) if(selIdx==0) return; // move the subitems for the previous row // to cache to make room for the selected row for(int i=0; i < lv.Items[selIdx].SubItems.Count; i++) { cache = lv.Items[selIdx - 1].SubItems[i].Text; lv.Items[selIdx - 1].SubItems[i].Text = lv.Items[selIdx].SubItems[i].Text; lv.Items[selIdx].SubItems[i].Text = cache; } lv.Items[selIdx-1].Selected=true; lv.Refresh(); lv.Focus(); } else { // ignore movedown of last item if(selIdx==lv.Items.Count-1) return; // move the subitems for the next row // to cache so we can move the selected row down for(int i = 0; i < lv.Items[selIdx].SubItems.Count; i++) { cache = lv.Items[selIdx + 1].SubItems[i].Text; lv.Items[selIdx + 1].SubItems[i].Text = lv.Items[selIdx].SubItems[i].Text; lv.Items[selIdx].SubItems[i].Text = cache; } lv.Items[selIdx+1].Selected=true; lv.Refresh(); lv.Focus(); } } |