VB如何清除listview中的现有项目

时间:2018-04-24 17:44:20

标签: vb.net listview

我正在尝试从sql server搜索信息,然后在listview中显示。每次我点击搜索,它都不会根据过去的时间搜索清除现有记录。我已经尝试过item.clear,它只会向我显示新搜索,但它不会清除所有现有记录。有人可以帮忙吗?

   Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
    Dim strNameSource As String
    Dim strPhoneSource As String
    Dim strBothSource As String

    If OpenConnect() = True Then
        Try
            If ChkCustName.Checked = True Then

                strNameSource = "SELECT TCI.strLastName + ', ' + TCI.strFirstName AS strName,TCI.strPhoneNumber,CONVERT(VARCHAR,TCI.dtmCheckIn, 101),CONVERT(VARCHAR,TCI.dtmCheckOut,101),TRT.strRoomType,TR.strRoom FROM TCheckInInfo AS TCI,TRoom AS TR,TRoomType AS TRT WHERE TCI.intRoomID = TR.intRoomID AND TR.intRoomTypeID = TRT.intRoomTypeID AND intCheckInStatusID = 1 AND TCI.strLastName ='" & txtLastName.Text & "'"
                SearchReservation(strNameSource)
            ElseIf ChkPhoneNumber.Checked = True Then
                strPhoneSource = "SELECT TCI.strLastName + ', ' + TCI.strFirstName AS strName,TCI.strPhoneNumber,CONVERT(VARCHAR,TCI.dtmCheckIn, 101),CONVERT(VARCHAR,TCI.dtmCheckOut,101),TRT.strRoomType,TR.strRoom FROM TCheckInInfo AS TCI,TRoom AS TR,TRoomType AS TRT WHERE TCI.intRoomID = TR.intRoomID AND TR.intRoomTypeID = TRT.intRoomTypeID AND intCheckInStatusID = 1 AND TCI.strPhoneNumber ='" & txtPhoneNumber.Text & "'"
                SearchReservation(strPhoneSource)
            ElseIf ChkCustName.Checked = True And ChkPhoneNumber.Checked = True Then
                strBothSource = "SELECT TCI.strLastName + ', ' + TCI.strFirstName AS strName,TCI.strPhoneNumber,CONVERT(VARCHAR,TCI.dtmCheckIn, 101),CONVERT(VARCHAR,TCI.dtmCheckOut,101),TRT.strRoomType,TR.strRoom FROM TCheckInInfo AS TCI,TRoom AS TR,TRoomType AS TRT WHERE TCI.intRoomID = TR.intRoomID AND TR.intRoomTypeID = TRT.intRoomTypeID AND intCheckInStatusID = 1 AND TCI.strPhoneNumber ='" & txtPhoneNumber.Text & "' AND TCI.strLastName ='" & txtLastName.Text & "'"
                SearchReservation(strBothSource)
            End If
            txtLastName.Clear()
            txtPhoneNumber.Clear()
        Catch excError As Exception
            WriteLog(excError)
            'End program
            Application.Exit()
        End Try
    End If

End Sub


Private Sub SearchReservation(ByVal strSource As String)
    Dim itemcollection(100) As String
    Dim Row As Integer
    Dim Column As Integer
    Dim ListViewItem As New ListViewItem

    lstReservation.Items.Clear()

    Try

        cmd = New OleDb.OleDbCommand(strSource, cnn)
        Adapter = New OleDb.OleDbDataAdapter(cmd)
        Adapter.Fill(Ds, "Table")

        'Now adding the Items in Listview

        If Ds.Tables(0).Rows.Count = 0 Then
            ' Something went wrong. warn user
            MessageBox.Show(Me, "Could not find the Customer", "Customer finding Error", _
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            lstReservation.BeginUpdate()

            For Row = 0 To Ds.Tables(0).Rows.Count - 1

                For Column = 0 To Ds.Tables(0).Columns.Count - 1

                    itemcollection(Column) = Ds.Tables(0).Rows(Row)(Column).ToString()
                Next
                ListViewItem = New ListViewItem(itemcollection)
                lstReservation.Items.Add(ListViewItem)
            Next
            lstReservation.EndUpdate()

        End If
    Catch excError As Exception
        WriteLog(excError)
        'End program
        Application.Exit()
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

您的代码存在很多问题,但我会专注于您的问题。

您继续使用 new 表填充DataSet,但是您继续使用仍在DataSet的零位置的原始表。

我猜想你需要清除DataTable的行:

If Ds.Tables.Contains("Table") Then
  Ds.Tables("Table").Rows.Clear()
End If
lstReservation.Items.Clear()

只清除那里的任何表格:

Ds.Tables.Clear()
lstReservation.Items.Clear()