ListView sort and move

Source: Internet
Author: User

Http://topic.csdn.net/t/20041001/07/3422642.html

 

Method 1:
Exchange content between two nodes

Method 2:
Obtain the selected node first:
Dim item As ListViewItem = Me. ListView1.SelectedItems (0)
Dim index As Integer = Me. ListView1.SelectedItems (0). Index

Delete the node:
ListView1.Items. Remove (item)

Re-insert the node:
If you move up: ListView. Items. Insert (index-1, item)

If you move down: ListView. Items. Insert (index + 1, item)

==============================================


Public Class frm
Inherits System. Windows. Forms. Form

# Region "Windows Form Designer generated code"

Public Sub New ()
MyBase. New ()

'This call is required by the Windows Form Designer.
InitializeComponent ()

'Add any initialization after the InitializeComponent () call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
Components. Dispose ()
End If
End If
MyBase. Dispose (disposing)
End Sub

'Required by the Windows Form Designer
Private components As System. ComponentModel. IContainer

'Note: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnUp As System. Windows. Forms. Button
Friend WithEvents btnDown As System. Windows. Forms. Button
Friend WithEvents lvw As System. Windows. Forms. ListView
<System. Diagnostics. DebuggerStepThrough ()> Private Sub InitializeComponent ()
Me. lvw = New System. Windows. Forms. ListView
Me. btnUp = New System. Windows. Forms. Button
Me. btnDown = New System. Windows. Forms. Button
Me. SuspendLayout ()
'
'Lvw
'
Me. lvw. Location = New System. Drawing. Point (12, 12)
Me. lvw. Name = "lvw"
Me. lvw. Size = New System. Drawing. Size (284,212)
Me. lvw. TabIndex = 0
'
'Btnup
'
Me. btnUp. Location = New System. Drawing. Point (308, 24)
Me. btnUp. Name = "btnUp"
Me. btnUp. Size = New System. Drawing. Size (40, 28)
Me. btnUp. TabIndex = 2
Me. btnUp. Text = "Up"
'
'Btndown
'
Me. btnDown. Location = New System. Drawing. Point (308, 60)
Me. btnDown. Name = "btnDown"
Me. btnDown. Size = New System. Drawing. Size (40, 28)
Me. btnDown. TabIndex = 3
Me. btnDown. Text = "Down"
'
'Frm
'
Me. AutoScaleBaseSize = New System. Drawing. Size (6, 14)
Me. ClientSize = New System. Drawing. Size (360,245)
Me. Controls. Add (Me. btnDown)
Me. Controls. Add (Me. btnUp)
Me. Controls. Add (Me. lvw)
Me. Name = "frm"
Me. ResumeLayout (False)

End Sub

# End Region

Private Sub frm_Load (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles MyBase. Load
InitLVW ()
End Sub

'Add-ons to the list
Private Sub InitLVW ()
Dim I As Integer
Me. lvw. FullRowSelect = True
Me. lvw. View = View. Details
Me. lvw. Columns. Add ("ID", 100, HorizontalAlignment. Center)
Me. lvw. Columns. Add ("Name", 100, HorizontalAlignment. Center)
For I = 0 To 10
Dim xItem As New ListViewItem
XItem. Text = I. ToString ()
XItem. SubItems. Add (I & "item ")
Me. lvw. Items. Add (xItem)
Next
End Sub

'Move up
Private Sub btnUp_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles btnUp. Click
'The operation can be performed only when the list item has more than one record and a selected record
Dim iCount As Integer = Me. lvw. Items. Count
If iCount> 1 And Me. lvw. SelectedItems. Count> 0 Then
'You can move up only when not 1st items are selected
If Me. lvw. SelectedItems (0). Index <> 0 Then
Dim iIndex As Integer = lvw. SelectedItems (0). Index

Dim xItem As ListViewItem = Me. lvw. SelectedItems (0)
Lvw. Items. Remove (xItem)
Lvw. Items. Insert (iIndex-1, xItem)
End If
End If
End Sub

'Move down
Private Sub btnDown_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles btnDown. Click
'The operation can be performed only when the list item has more than one record and a selected record
Dim iCount As Integer = Me. lvw. Items. Count
If iCount> 1 And Me. lvw. SelectedItems. Count> 0 Then
'Move down only when the selected item is not the last one.
If Me. lvw. SelectedItems (0). Index <> iCount-1 Then
Dim iIndex As Integer = lvw. SelectedItems (0). Index

Dim xItem As ListViewItem = Me. lvw. SelectedItems (0)
Lvw. Items. Remove (xItem)
Lvw. Items. Insert (iIndex + 1, xItem)
End If
End If
End Sub

End Class
========================================================== =

 

Move up:
Private void button#click (object sender, System. EventArgs e)
{
For (int I = 0; I <listView1.CheckedItems. Count; I ++)
{
If (listView1.CheckedItems [I]. Index! = 0)
{
ListViewItem item = listView1.CheckedItems [I];
Int index = item. Index;
ListView1.Items. Remove (item );
ListView1.Items. Insert (index-1, item );
}
}
}

Move down:
Private void button2_Click (object sender, System. EventArgs e)
{
For (int I = listView1.CheckedItems. Count; I> 0; I --)
{
If (listView1.CheckedItems [the I-1]. Index! = ListView1.Items. Count-1)
{
ListViewItem item = listView1.CheckedItems [I-1];
Int index = item. Index;
ListView1.Items. Remove (item );
ListView1.Items. Insert (index + 1, item );
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.