I have a listview that is filled with an sql query from temp Sql DB that is created from a program that run from textbox inputs, which writes this db. When the select button is pushed it query loads the listview. Sometimes their may be "0" or null values in at lest three columns. I have tried a Do until count of listview to do a remove() of the index where those 0 values appear in listview, but can not get it to work. Copy of code

Dim indexes As ListView.SelectedIndexCollection = OS_Form.ListView2.SelectedIndices
Dim index As Integer
Dim rcount As Integer

rcount = OS_Form.sh_listview2.Items.Count
For index = 1 To rcount
If index > rcount Then
Exit For
If OS_Form.sh_listview2.Items.Item(X).SubItems(3).Text = "0" Then
OS_Form.sh_listview2.Items(X).Remove()
End If

If OS_Form.sh_listview2.Items.Item(X).SubItems(6).Text > "10" Then
OS_Form.sh_listview2.Items(X).Remove()
End If
End If
Next

Any help please.

for i = listview2.selectedindices.count - 1 to 0 step -1
if listview2.items(listview2.selectedindices(i)).subitems(3).text = "0" orelse listview2.items(listview2.selectedindices(i)).subitems(6).text = "10" then listview2.items(listview2.selectedindices(i)).remove
next

for i = listview2.selectedindices.count - 1 to 0 step -1
if listview2.items(listview2.selectedindices(i)).subitems(3).text = "0" orelse listview2.items(listview2.selectedindices(i)).subitems(6).text = "10" then listview2.items(listview2.selectedindices(i)).remove
next

It did not work. I get the same results. The values are still listed in the listview.

I just copied and pasted the code and ran it. It works just fine. Make sure you have your sub item numbers correct and that you have items selected.


If your not working with selected items then use this

for i = listview2.items.count - 1 to 0 step -1
if listview2.items(i).subitems(3).text = "0" orelse listview2.items(i).subitems(6).text = "10" then listview2.items(i).remove
next

Ok here is my code for this:
Private Sub Button_Select_Model_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Select_Model.Click
If Not IsNothing(OS_Form) Then
If Not OS_Form.IsDisposed Then
OS_Form.WindowState = FormWindowState.Normal
OS_Form.BringToFront()
OS_Form.Visible = True
Else
OS_Form = New OS_Form
OS_Form.Left = Me.Width
OS_Form.Top = 0
OS_Form.Visible = True
End If
Else
OS_Form = New OS_Form
OS_Form.Left = Me.Width
OS_Form.Top = 0
OS_Form.Visible = True
End If

Select Case Temp_CBox.Text
Case "95-85-78"
TEMP_CD = "FJM"
Case "90-80-64"
TEMP_CD = "GLV"
Case "90-80-66"
TEMP_CD = "GLU"
Case "90-80-68"
TEMP_CD = "GLT"
Case "90-80-70"
TEMP_CD = "GLS"
Case "90-80-72"
TEMP_CD = "GLR"
Case "95-85-75"
TEMP_CD = "FKP"
Case "95-85-76"
TEMP_CD = "FKN"
End Select

ssql_GPM = "SELECT * FROM GPM_Ratings"
ssql_GPM = ssql_GPM + " WHERE FillHt = '" + sFillHt + "'"
ssql_GPM = ssql_GPM + " AND AIRTR = '" + sAIRTR + "'"

Dim Design_Form As New Design_Form
With Design_Form
.DC_P3K.Open()
.DS_GPM_Rates1.Clear()
.DA_Rating.SelectCommand.CommandText = ssql_GPM
.DA_Rating.Fill(.DS_GPM_Rates1)
Dim str(9) As String


Dim listitem As ListViewItem
OS_Form.sh_listview2.Visible = True
OS_Form.sh_listview2.Items.Clear()

If .DS_GPM_Rates1.GPM_Ratings.Rows.Count <> 0 Then

X = 0
Do

If TEMP_CD = "FJM" Then
str(0) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).Model.ToString()
str(1) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).HP.ToString()
str(3) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).FJM.ToString()

End If

If TEMP_CD = "GLV" Then
str(0) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).Model.ToString()
str(1) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).HP.ToString()
str(3) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).GLV.ToString()
End If

If TEMP_CD = "GLU" Then
str(0) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).Model.ToString()
str(1) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).HP.ToString()
str(3) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).GLU.ToString()
End If

If TEMP_CD = "GLT" Then
str(0) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).Model.ToString()
str(1) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).HP.ToString()
str(3) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).GLT.ToString()
End If

If TEMP_CD = "GLS" Then
str(0) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).Model.ToString()
str(1) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).HP.ToString()
str(3) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).GLS.ToString()
End If

If TEMP_CD = "GLR" Then
str(0) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).Model.ToString()
str(1) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).HP.ToString()
str(3) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).GLR.ToString()
End If

If TEMP_CD = "FKP" Then
str(0) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).Model.ToString()
str(1) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).HP.ToString()
str(3) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).FKP.ToString()
End If

If TEMP_CD = "FKN" Then
str(0) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).Model.ToString()
str(1) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).HP.ToString()
str(3) = Design_Form.DS_GPM_Rates1.GPM_Ratings(X).FKN.ToString()
End If


listitem = New ListViewItem(str)
OS_Form.sh_listview2.Items.Add(listitem)
OS_Form.sh_listview2.Items.Item(X).SubItems(4).Text = CInt(CDbl(OS_Form.sh_listview2.Items.Item(X).SubItems(3).Text) / 3)
OS_Form.sh_listview2.Items.Item(X).SubItems(6).Text = Math.Ceiling(GPMtwr.Text / OS_Form.sh_listview2.Items.Item(X).SubItems(3).Text)
OS_Form.sh_listview2.Items.Item(X).SubItems(5).Text = OS_Form.sh_listview2.Items.Item(X).SubItems(6).Text * OS_Form.sh_listview2.Items.Item(X).SubItems(3).Text

' Here is where I put your code
For i = OS_Form.sh_listview2.Items.Count - 1 To 0 Step -1
If OS_Form.sh_listview2.Items(i).SubItems(3).Text = "0" OrElse OS_Form.sh_listview2.Items(i).SubItems(6).Text = "10" Then OS_Form.sh_listview2.Items(i).Remove()
Next

X += 1
Loop Until X = .DS_GPM_Rates1.GPM_Ratings.Rows.Count
End If


End With

End Sub
When the button is pushed it crashes out of program. Help please

Solved by adding another for loop after the listview step

Thanks for your help

This article has been dead for over six months. Start a new discussion instead.