2014-01-06 3 views
0

Я заполняю элементы в listview, и когда я нажимаю кнопку команды, я добавлю элемент в список в базе данных, и если элемент из списка не был извлечен, он удалит элемент из заполненного списка ниже мой код.Как удалить элементы в списке? vb.net

Dim y As Integer 
Dim a As String 
y = ListView2.Items.Count 
While y >= 0 
    a = ListView2.Items.Item(y).Text 
    y = y - 1 

    Dim TMP_SQL_VAL As String = "select count([Check-Out]) from tbl_list1 where barcode = '" + a + "'" 
    locconn.Open() 
    command = New SqlCommand(TMP_SQL_VAL, locconn) 
    Dim READER As SqlDataReader 
    READER = command.ExecuteReader() 
    READER.Read() 

    If READER(0) = 0 Then 
     MsgBox("Barcode: " & a & "is still Inside", MsgBoxStyle.Exclamation) 
     clear_text() 

     ListView2.Items.Remove(y) 
    Else 
     READER.Close() 
     Dim READER2 As SqlDataReader 
     Dim TMP_SQL_VAL2 = "select [Check-In] from tbl_list1 where barcode = '" + a + "' and [User] = '" + rsuser + "'" 
     Dim cmd = New SqlCommand(TMP_SQL_VAL2, locconn) 
     READER2 = cmd.ExecuteReader 
     READER2.Read() 
     If READER2(0) Is Nothing Then 
      MsgBox("Barcode: " & a & "is still Inside", MsgBoxStyle.Exclamation) 
      clear_text() 
     ListView2.Items.Remove(y) 
     End If 
    end if 
    locconn.Close() 
End While 

Catch ex As Exception 
    MsgBox(ex.Message) 
    localconn.ShowDialog() 
Finally 
    locconn.Close() 
End Try 
+0

http://www.codeproject.com/Tips/170900/ Как использовать-Delete-Selected-Items-of-ListView-and-ListB использовать эту ссылку ... –

+1

Вы должны использовать RemoveAt вместо Remove в этом случае, потому что вы используете y как индекс. RemoveAt удаляет элемент по указанному индексу. Удалить удаляет сам элемент, для которого вам нужно использовать какой-либо тип. Для каждого из них нужно закодировать все элементы в списке, чтобы получить сам ListViewItem. Я просто пытался помочь, я просто думаю, что на это можно ответить. – SomeNickName

+0

, но я продолжаю получать ошибку «InvalidArguments = значение« -1 »недопустимо для« индекса ». Индекс имени параметра« – Ron

ответ

0

Попробуйте проверить, если значение у < 0, если это не то ничего, чтобы обработать его прекратить

Dim y As Integer 
Dim a As String 
y = ListView2.Items.Count - 1 'Reduce 1 
If y < 0 Then return 'If no item then terminate 
While y >= 0 
    a = ListView2.Items.Item(y).Text 
    'y = y - 1 'Moved to last line before End While 
    Dim TMP_SQL_VAL As String = "select count([Check-Out]) from tbl_list1 where barcode = '" + a + "'" 
    locconn.Open() 
    command = New SqlCommand(TMP_SQL_VAL, locconn) 
    Dim READER As SqlDataReader 
    READER = command.ExecuteReader() 
    READER.Read() 

    If READER(0) = 0 Then 
     MsgBox("Barcode: " & a & "is still Inside", MsgBoxStyle.Exclamation) 
     clear_text() 

     ListView2.Items.Remove(y) 
    Else 
     READER.Close() 
     Dim READER2 As SqlDataReader 
     Dim TMP_SQL_VAL2 = "select [Check-In] from tbl_list1 where barcode = '" + a + "' and [User] = '" + rsuser + "'" 
     Dim cmd = New SqlCommand(TMP_SQL_VAL2, locconn) 
     READER2 = cmd.ExecuteReader 
     READER2.Read() 
     If READER2(0) Is Nothing Then 
      MsgBox("Barcode: " & a & "is still Inside", MsgBoxStyle.Exclamation) 
      clear_text() 
     ListView2.Items.Remove(y) 
     End If 
    end if 
    locconn.Close() 

    y -= 1 'Move here so the value is decremented before the While statement 

End While 

Catch ex As Exception 
    MsgBox(ex.Message) 
    localconn.ShowDialog() 
Finally 
    locconn.Close() 
End Try 
Смежные вопросы