Question : Error assigng data to a list view

Hello,

I am trying to assign a selected item from a listbox to a list view.  The list view has four columns.  I am able to assign values to the item and first two subitems of the list view with no problem but keep getting the following error message when attempting to assign a value to the third subitem
ex.Message = "InvalidArgument=Value of '3' is not valid for 'index'. Parameter name: index"
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
Private Sub btnAssignMetric_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssignMetric.Click
        Try
            If lstMetrics.SelectedIndex = -1 Then
                MessageBox.Show("You must select a Metric that will be assigned from the Select a Metric list box", "Missing Selection")
                Exit Sub
            End If
            If lvMatchedDvid.SelectedItems.Count < 1 Then
                MessageBox.Show("You must select at least one dataset pair to assign a Metric Name to", "Missing Selection")
                cboAssignSheetTabId.SelectedIndex = -1
                Exit Sub
            End If

            For Each item As ListViewItem In lvMatchedDvid.SelectedItems
                item.SubItems(3).Text = lstMetrics.SelectedItem.ToString
               
            Next


        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.GetType.ToString)
        End Try
    End Sub

Answer : Error assigng data to a list view

That's odd...
When you debug, can you tell how many sub items in the item object, try something like this:

           For Each item As ListViewItem In lvMatchedDvid.SelectedItems
   messagebox.Show(item.SubItems.Count)

               item.SubItems(item.SubItems.Count-1).Text = lstMetrics.SelectedItem.ToString
             
            Next

Random Solutions  
 
programming4us programming4us