我目前正在使用以下查询从访问数据库填充列表框。我希望能够过滤结果并从列表框中删除文本文件中列出的任何项目
Dim da As New OleDb.OleDbDataAdapter("", "")
Dim dt As New DataTable
Dim conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.aClients & ""
Dim n As Integer
Dim eSearch As String = AllDetails(n).uCode
Dim fSearch As String = AllDetails(n).uOps
da.SelectCommand.Connection.ConnectionString = conn
da.SelectCommand.CommandText = "SELECT Documents.DocName FROM Documents WHERE (Documents.UnitCode = ?) AND (Documents.OpName = ?) AND Documents.Required = True ORDER BY DocName"
da.SelectCommand.Parameters.AddWithValue("@p1", eSearch)
da.SelectCommand.Parameters.AddWithValue("@p2", fSearch)
da.Fill(dt)
dt.Rows.Add("Add Additional Requirement")
lstRequired.DataSource = dt
lstRequired.DisplayMember = "DocName"
lstRequired.Refresh()
在使用2个未被查询填充的列表框时,我使用下面的代码用于相同的目的但是我似乎无法与查询一起实现它
Dim R As IO.StreamReader
R = New IO.StreamReader(tFiles & "PlanExcluded.txt")
While (R.Peek() > -1)
lstRequired.Items.Remove(R.ReadLine)
End While
R.Close()
有人能指出我正确的方向吗?
由于
答案 0 :(得分:1)
这是因为Items是DataRow的集合,而不是字符串的集合 为了能够删除特定项目,您应该使用绑定到ListBox(数据表)的DataSource
Dim dt as DataTable = CType(lstRequired.DataSource, DataTable)
Using R = New IO.StreamReader(tFiles & "PlanExcluded.txt")
While (R.Peek() > -1)
Dim rows() = dt.Select("DocName = '" + r.ReadLine + "'")
For Each row in rows
row.Delete()
Next
dt.AcceptChanges()
End While
End Using
如果要删除的DocName数据包含带单引号的字符串,请记住将单引号加倍
Dim rows() = dt.Select("DocName = '" + r.ReadLine.Replace("'", "''") + "'")