ListView 排序 移动
http://topic.csdn.net/t/20041001/07/3422642.html
方法1:
上下两个节点交换一下内容
方法2:
先取得选中的节点:
Dim item As ListViewItem = Me.ListView1.SelectedItems(0)
Dim index As Integer = Me.ListView1.SelectedItems(0).Index
然后删除该节点:
ListView1.Items.Remove(item)
重新插入该节点:
如果上移:ListView.Items.Insert(index-1,item)
如果下移: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
'加载项到列表
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
'上移
Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click
'当列表项有多于1条记录,并且有选中记录时,才能操作
Dim iCount As Integer = Me.lvw.Items.Count
If iCount > 1 And Me.lvw.SelectedItems.Count > 0 Then
'当选择中的不是第1项时才可上移
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
'下移
Private Sub btnDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDown.Click
'当列表项有多于1条记录,并且有选中记录时,才能操作
Dim iCount As Integer = Me.lvw.Items.Count
If iCount > 1 And Me.lvw.SelectedItems.Count > 0 Then
'当选择中的不是最后项时才可下移
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
==========================================
上移:
private void button1_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);
}
}
}
下移:
private void button2_Click(object sender, System.EventArgs e)
{
for(int i = listView1.CheckedItems.Count; i > 0; i--)
{
if(listView1.CheckedItems[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);
}
}