我有一个ListView
,有几个项目。我想将这些项目传递给DataGridView
,但我一直收到此错误:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll
Additional information: InvalidArgument=Value of '14' is not valid for 'index'.
这是我使用的代码:
Dim num As Integer
num = 0
While (num <= ListView1.Items.Count)
With ListView1.Items(num)
Dim lvItem() As String = {.Text, .SubItems(1).Text, .SubItems(2).Text, .SubItems(3).Text, .SubItems(4).Text, .SubItems(5).Text, .SubItems(6).Text} '// get ListView selectedItem.
DataGridView1.Rows.Add(lvItem) '// add it to DataGridView.
End With
num = num + 1
End While
答案 0 :(得分:1)
使用(num < ListView1.Items.Count)
代替While (num <= ListView1.Items.Count)
答案 1 :(得分:0)
如果你循环所有收藏,我会建议使用For Each
有了这个,您不必担心索引并使用索引号的额外变量
For Each item As ListViewItem In ListView1.Items
With item
Dim lvItem() As String = {.Text,
.SubItems(1).Text,
.SubItems(2).Text,
.SubItems(3).Text,
.SubItems(4).Text,
.SubItems(5).Text,
.SubItems(6).Text} '// get ListView selectedItem.
DataGridView1.Rows.Add(lvItem) '// add it to DataGridView.
End With
Next