在 vb.net 中使用组合框过滤列表视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17465363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filter a Listview with a combobox in vb.net
提问by shaik ibrahim
What i want is mostly to hide arrays not requeries. So i have a combobox and listbox with values from database done by adapter , dataset , datatable and binding source. . SO when the person change the combobox values it would filter the list .So lets say to combobox is containing the ID so the list view will show those rows with that id. Onload the listview shows all properties . So how do you do this without Requery ???? So below i have the populating list and combobox part.
我想要的主要是隐藏数组而不是重新查询。所以我有一个组合框和列表框,其中包含由适配器、数据集、数据表和绑定源完成的数据库中的值。. 因此,当此人更改组合框值时,它将过滤列表。因此,可以说组合框包含 ID,因此列表视图将显示具有该 ID 的那些行。Onload 列表视图显示所有属性。那么你如何在没有 Requery 的情况下做到这一点????所以下面我有填充列表和组合框部分。
here is a link but its not very helpfull VB.NET Listview Multiple Column Filter
这是一个链接,但它不是很有帮助 VB.NET Listview Multiple Column Filter
Dim listcount As Integer = listview1.Columns.Count
With listview1
.Columns.Clear()
.View = View.Details
.GridLines = True
.Columns.Add("Name").Width = 70
.Columns.Add("ID").Width = 60
.Columns.Add("Number").Width = 90
End With
Try
strQuery = "Select * From Table"
DB.Connection = New SqlConnection(strConnection)
DB.Connection.Open()
' add a daaset
Adapter = New SqlDataAdapter(strQuery, DB.Connection)
ListRS = New DataSet
Adapter.Fill(ListRS)
Dim table As DataTable = ListRS.Tables(0)
bnsrc = New BindingSource
bnsrc.DataSource = ListRS.Tables(0)
combobox1.DataSource = bnsrc
combobox1.DisplayMember = "ID"
combobox1.ValueMember = "ID"
combobox1.AutoCompleteSource = AutoCompleteSource.ListItems
Listview1.Items.Clear()
DB.Connection.Close()
So if i we to do requery it would be the same ust change the sql statement .
因此,如果我要重新查询,则更改 sql 语句将是相同的。
strQuery = "Select * from Table Where ID=@id"
IF i added a parameter how ever it would effect the combobx because they are using the same queries.
如果我添加了一个参数,它会如何影响 combobx,因为它们使用相同的查询。
采纳答案by varocarbas
You can populate Listview1on the SelectedIndexChangedevent of comboxbox1by adding the items you want every time. Example:
可以填充Listview1在SelectedIndexChanged事件comboxbox1中加入每次你想的项目。例子:
Private Sub combobox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles comboBox1.SelectedIndexChanged
ListView1.Items.Clear()
Dim curSelectionCombobox As String = combobox1.SelectedItem.ToString()
For Each item As String In allItems
If (condition) Then
ListView1.Items.Add(item)
End If
Next
End Sub
With the code above, you get the currently-selected item in combobox1(curSelectionCombobox) and also iterate through all the elements retrieved from the database and decide (on account of the given condition) which ones should/shouldn't be added. Thus, this approach does not use any filtering, but performs a from-scratch addition of elements every time the selection of combobox1changes.
使用上面的代码,您可以在combobox1( curSelectionCombobox) 中获取当前选择的项目,并遍历从数据库中检索到的所有元素并决定(根据给定条件)应该/不应该添加哪些元素。因此,这种方法不使用任何过滤,而是在每次选择combobox1更改时从头开始添加元素。
If you want to perform the "filtering" just at form1 load
如果您只想在 form1 加载时执行“过滤”
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ListView1.Items.Clear()
For Each item As String In allItems
If (condition) Then
ListView1.Items.Add(item)
End If
Next
End Sub
If you want to change the items in the Listview1 at runtime based on various conditions, you should rely on controls allowing multiselection (e.g., checkboxes). Sample code:
如果您想在运行时根据各种条件更改 Listview1 中的项目,您应该依赖允许多选的控件(例如,复选框)。示例代码:
Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
If (CheckBox1.Checked And CheckBox2.Checked And CheckBox3.Checked) Then
ListView1.Items.Clear()
For Each item As String In allItems
If (condition) Then
ListView1.Items.Add(item)
End If
Next
End If
End Sub
Thus, the idea is not adding all the items to the Listbox1 and then filtering them, but just adding the items you want. Where I put "condition" you can call a function performing as complex checks as you wish.
因此,这个想法不是将所有项目添加到 Listbox1 然后过滤它们,而只是添加您想要的项目。在我放置“条件”的地方,您可以调用一个函数,根据需要执行复杂的检查。

