[英]Apply filters from multiple checkboxList to filter ListView in VB.net
I have webapplication where there are 1000 of products so there is multiple filter needs to be there. 我的Web应用程序中有1000种产品,因此需要多个过滤器。 I have total 4 checkboxList.
我总共有4个checkboxList。 Now my problem is if I apply filter from any of checkboxList then it checks selected value for all column that is specified in query.
现在我的问题是,如果我从checkboxList中的任何一个应用过滤器,那么它将检查查询中指定的所有列的选定值。 What I want is like below
我想要的是下面的样子
SELECT * FROM products WHERE price_rang IN ('selectedValueFromCheckBoxList1') And category IN ('selectedValueFromCheckBoxList2')
What is happening now 现在发生了什么
SELECT * FROM `products` WHERE price_range IN ('selectedValueFromCheckBoxList2') AND category IN ('selectedValueFromCheckBoxList2')
So in this query suppose If I select value First from checkboxList2 then for both the columns it takes that value & result not displaying. 因此,在此查询中,假设如果我从checkboxList2中选择值First,那么对于这两个列,它都将采用该值并不会显示结果。
Below is my filter process code 下面是我的过滤过程代码
Private Sub getResult()
Dim constr As String = ConfigurationManager.ConnectionStrings("conio").ConnectionString
Dim query As String = "select * from products"
Dim condition As String = String.Empty
For Each price As ListItem In priceFilter.Items
condition += If(price.Selected, String.Format("'{0}',", price.Value), String.Empty)
Next
For Each sub_category As ListItem In category.Items
condition += If(sub_category.Selected, String.Format("'{0}',", sub_category.Value), String.Empty)
Next
If Not String.IsNullOrEmpty(condition) Then
condition = String.Format(" WHERE price_range IN ({0}) and sub_category IN ({0})", condition.Substring(0, condition.Length - 1))
End If
Using con As New MySqlConnection(constr)
Using cmd As New MySqlCommand(query & condition)
Using sda As New MySqlDataAdapter(cmd)
cmd.Connection = con
Using dt As New DataTable()
sda.Fill(dt)
products.DataSource = dt
products.DataBind()
End Using
End Using
End Using
End Using
End Sub
This is an option is you're only interested in the checked items. 这是一个选项,因为您只对选中的项目感兴趣。
Public Function buildWhereClause() As String
Dim query As String = "select * from products"
Dim joiner As String = " "
Dim condition As String = String.Empty
Dim priceCondition As String = String.Empty
For i = 0 To priceFilter.Items.Count - 1
If priceFilter.Items(i).Selected Then
Dim price As String = priceFilter.Items(i).ToString
priceCondition = String.Concat(priceCondition, joiner, String.Format("'{0}'", price))
If joiner = " " Then joiner = ", "
End If
Next
Dim categoryCondition As String = String.Empty
joiner = " "
For i = 0 To categoryFilter.Items.Count - 1
If categoryFilter.Items(i).Selected Then
Dim category As String = categoryFilter.Items(i).ToString
categoryCondition = String.Concat(categoryCondition, joiner, String.Format("'{0}'", category))
If joiner = " " Then joiner = ", "
End If
Next
Dim whereClause As String = String.Empty
joiner = " where "
If Not String.IsNullOrEmpty(priceCondition) Then
whereClause = String.Concat(whereClause, joiner, String.Format(" price_range IN ({0})", priceCondition)) ' and sub_category IN ({0})", condition.Substring(0, condition.Length - 1))
joiner = " and "
End If
If Not String.IsNullOrEmpty(categoryCondition) Then
whereClause = String.Concat(whereClause, joiner, String.Format(" sub_category in ({0})", categoryCondition))
joiner = " and "
End If
Return String.Concat(query, whereClause)
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.