Public Class ListViewOperation
Shared bRowsNum As Boolean = False
'初期化ListView
‘lstCTL :ListView控件变量
’columns() :一维字符串数组,ListView的列标题
Public Shared Sub list_init(ByRef lstCTL As System.Windows.Forms.ListView, ByVal columns() As String, Optional ByVal RowNum As Boolean = False)
If lstCTL Is Nothing Then
Exit Sub
End If
Dim lstComm As System.Windows.Forms.ListView = lstCTL
'clear old data
lstComm.Items.Clear()
lstComm.Columns.Clear()
lstComm.View = View.Details
lstComm.MultiSelect = True
lstComm.FullRowSelect = True
lstComm.HideSelection = False
Dim strColumn As String
'add RowNum
bRowsNum = RowNum
If bRowsNum Then
lstComm.Columns.Add(" ", 30)
End If
If columns Is Nothing Then
lstComm.View = View.List
lstComm.MultiSelect = False
Exit Sub
End If
'set column name
For Each strColumn In columns
If strColumn IsNot Nothing Then
If strColumn.Trim.Equals("") Then
Continue For
End If
lstComm.Columns.Add(strColumn, strColumn.Length * 6 + 50)
End If
If strColumn Is Nothing Then
Exit For
End If
Next
'set gridline type
lstComm.GridLines = True
End Sub
'''
''' 从ListView最后一行开始添加数据行
'''
''' ListView控件变量
''' 二维数组 第二维是行,第一维是一行的所有列
'''
Public Shared Sub list_addData(ByRef lstCTL As System.Windows.Forms.ListView, ByVal strItems()() As String)
If strItems Is Nothing Then
Exit Sub
End If
If lstCTL Is Nothing Then
Exit Sub
End If
Dim lstComm As System.Windows.Forms.ListView = lstCTL
Dim RowCounter As Integer
Dim ColCounter As Integer
Dim RowCurOrder As Integer = lstComm.Items.Count
Dim ColStartOrder As Integer
Dim bRowVaild As Boolean = False
Dim strColValue As String
Dim strInvaildRowCount As Integer = 0
' add row
For RowCounter = 0 To UBound(strItems)
If strItems(RowCounter) IsNot Nothing Then
bRowVaild = False
Dim listItem As System.Windows.Forms.ListViewItem
If bRowsNum Then ' add row number [column]
listItem = lstComm.Items.Insert(RowCounter + RowCurOrder, RowCurOrder.ToString)
ColStartOrder = 0
Else
'if row info is empty ,not append it
If strItems(RowCounter).ToString.Trim.Equals("") Then
Continue For
End If
'add info to last row
strColValue = strItems(RowCounter)(0)
listItem = lstComm.Items.Insert(RowCounter + RowCurOrder - strInvaildRowCount, strColValue)
ColStartOrder = 1
If Not String.IsNullOrEmpty(strColValue) Then
bRowVaild = True
End If
End If
'add column
For ColCounter = ColStartOrder To strItems(RowCounter).Length - 1
Dim listItemsubItem As New System.Windows.Forms.ListViewItem.ListViewSubItem
strColValue = strItems(RowCounter)(ColCounter)
listItemsubItem.Text = strColValue
listItem.SubItems.Insert(ColCounter, listItemsubItem)
If Not String.IsNullOrEmpty(strColValue) Then
bRowVaild = True
End If
Next ColCounter
'if row'a all column is empty , row is invaild ,so delete it
If Not bRowVaild Then
Dim count As Integer = lstComm.Items.Count
lstComm.Items.RemoveAt(count - 1)
strInvaildRowCount += 1
End If
End If
Next RowCounter
End Sub
'''
''' 取得ListView中的所有数据
'''
'''
'''
'''
Public Shared Function list_getData(ByRef lstCTL As System.Windows.Forms.ListView) As String()()
If lstCTL Is Nothing Then
Return Nothing
End If
Dim iCount As Integer = lstCTL.Items.Count
Dim strItems(iCount - 1)() As String
Dim iLoop As Integer = 0
Dim iLooop As Integer = 0
'get list view data info
For iLoop = 0 To iCount - 1
Dim strItem As String()
Dim strCol As String = CStr("")
Dim start As Integer = 0
'Show RowsNumber
If bRowsNum Then
start = 1
End If
'get one row info
For iLooop = start To lstCTL.Items.Item(iLoop).SubItems.Count - 1
If iLooop <> start Then
strCol += ";"
End If
strCol += lstCTL.Items.Item(iLoop).SubItems.Item(iLooop).Text
Next
strItem = Split(strCol, ";")
strItems(iLoop) = strItem
Next
Return strItems
End Function
'''
''' 取得ListView中选择的行当数据
'''
'''
'''
'''
Public Shared Function list_getSeledData(ByRef lstCTL As System.Windows.Forms.ListView) As String()()
If lstCTL Is Nothing Then
Return Nothing
End If
'get select row count
Dim iCount As Integer = lstCTL.SelectedItems.Count
Dim strItems(iCount - 1)() As String
Dim iLoop As Integer = 0
Dim iLooop As Integer = 0
'get select info
For iLoop = 0 To iCount - 1
Dim strItem As String()
Dim strCol As String = CStr("")
Dim start As Integer = 0
'Show RowsNumber
If bRowsNum Then
start = 1
End If
'get one row info
For iLooop = start To lstCTL.SelectedItems.Item(iLoop).SubItems.Count - 1
If iLooop <> start Then
strCol += ";"
End If
strCol += lstCTL.SelectedItems.Item(iLoop).SubItems.Item(iLooop).Text
Next
strItem = Split(strCol, ";")
strItems(iLoop) = strItem
Next
Return strItems
End Function
'''
''' 删除选择的行
'''
'''
'''
Public Shared Sub list_DelData(ByRef lstCTL As System.Windows.Forms.ListView)
Dim selCount As Integer = lstCTL.SelectedItems.Count
Dim iLoop As Integer
If selCount >= 1 Then
For iLoop = selCount - 1 To 0 Step -1
lstCTL.SelectedItems.Item(iLoop).Remove()
Next
End If
End Sub
'''
''' 删除listView中的所有数据
'''
'''
'''
Public Shared Sub list_DelAllData(ByRef lstCTL As System.Windows.Forms.ListView)
Dim selCount As Integer = lstCTL.Items.Count
Dim iLoop As Integer
If selCount >= 1 Then
For iLoop = selCount - 1 To 0 Step -1
lstCTL.Items.Item(iLoop).Remove()
Next iLoop
End If
End Sub
'''
''' 更新一行数据
'''
'''
''' 行号,从0开始
''' 修改的值对,列番号-列值一一对应
'''
Public Shared Sub list_UpdateSingleData(ByRef lstCTL As System.Windows.Forms.ListView, ByVal Index As Integer, ByVal mapColValue As Map)
Dim selCount As Integer = lstCTL.Items.Count
Dim iLoop As Integer
If Index < selCount Then
For iLoop = 0 To mapColValue.count - 1
lstCTL.Items.Item(Index).SubItems.Item(iLoop).Text = mapColValue.itemValue(iLoop)
' lstCTL.Items.Item(Index).SubItems.Item(mapColValue.itemKey(iLoop)) = listItemsubItem
Next iLoop
End If
End Sub
'''
''' delete listview all data info
'''
'''
'''
Public Shared Function list_GetSelIndex(ByRef lstCTL As System.Windows.Forms.ListView) As Integer
Dim selCount As Integer = lstCTL.Items.Count
Dim iRetValue As Integer = -1
If lstCTL.SelectedItems.Count > 0 Then
iRetValue = lstCTL.SelectedItems.Item(0).Index
End If
Return iRetValue
End Function
End Class