I've written a code where depending on some condition i have add item in list as

With lview
                .Items.Add(mydr(0))
                With .Items(.Items.Count - 1).SubItems
        if some condition then
                   .Add(mydr(9))
		   end if
		   end with
		   end with

But when i call listview click event for the if condition failure cases it gives error
InvalidArgument=Value of 9' is not valid for 'index'.
Parameter name: index
Is there any way that I can check if subitem exist or not.

Thanks

You are getting the error because mydr(9) is not declared and you probably have it declared as mydr(8) or less than 8.

To check if a ListView .SubItem exists, see if this helps.

Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
        With ListView1
            If Not .SelectedItems.Count = 0 Then '// check if item is selected.
                With .SelectedItems(0).SubItems(1)
                    If .Text = "" Then
                        MsgBox("No value for first SubItem, or No value in Column 2 for the selected item.")
                    Else
                        MsgBox("value found: " & .Text)
                    End If
                End With
            End If
        End With
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.