Reordering Items in a ListView in Response to Up or Down Button Click | | 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
|
Figure 3 show the code for the VB.NET MoveListViewItem method, which does all of the work of moving the selected item, up or down, based on the value of the moveUp Boolean. The methodology is very simple. First, I ensure that the user is not trying to move the bottom item up or the top item down. Next, I loop through the selected item (+/- 1) saving each cell in a variable named cache . As I move the contents of the cell to the temporary variable, I then move the corresponding cell from the selected item to the vacated cell. Finally, I move the contents of the cache variable to the vacated cell in the selected row.
Once all of the cells have been swapped, I select either the previous or next row, relative to the selected row, so that the user can see that the selected row moved and so they can continue to move the row if desired, without having to reselect it. Setting Focus to the ListView will allow the selection to be visible. Figure 4 shows the C# Code for the method.
Figure 3 - VB.NET Code for the MoveListViewItem.
''' <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
|
Top of Page
Figure 4 - C# Code for MoveListViewItems.
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();
}
}
| Top of Page
|