Reputation:
I'm trying to filter a DataGridView with multi criteria column in one textbox in vb.net.
The code below DoFilter2()
can only be used for column names Criteria1
so I can use for columns Criteria1
,COLORCODE
,SIZE
,INFO
Please Guide Me
Thanks
Private Sub DoFilter2()
BeginInvoke(
New Action(
Sub()
If TXTCRITERIAALLINONE.Text.Trim().Length = 0 Then
DataGridView1.DataSource = StockCardsCache
Return
End If
Dim dict = TXTCRITERIAALLINONE.Text.Split({","}, StringSplitOptions.None).
Take(4).
Select(Function(x, i) New With {.Index = i + 1, .Word = x.Trim()}).
Where(Function(x) Not String.IsNullOrEmpty(x.Word)).
ToDictionary(Function(pair) $"Criteria{pair.Index}",
Function(pair) pair.Word)
DataGridView1.DataSource = StockCardsCache.
Where(Function(StockCardsCache) StockCardsCache.IsFilterItem(dict)).ToList()
End Sub))
End Sub
End Class
Public Class StockCardsNEW
Public Property Invono() As String
Public Property InvoDate() As Date
Public Property Transaction() As String
Public Property CodeProduct() As String
Public Property Barcode() As String
Public Property Criteria1() As String
Public Property COLORCODE() As String
Public Property SIZE() As String
Public Property INFO() As String
Public Property [IN] As Integer
Public Property [OUT] As Integer
Public Property BLC As Integer
' Replace `.Equals` with `.Contains` if needed...
Public Function IsFilterItem(dict As IDictionary(Of String, String)) As Boolean
Return dict.All(
Function(kv) ContainsProperty(kv.Key) AndAlso
GetPropertyValue(Of String)(kv.Key).
Equals(kv.Value, StringComparison.CurrentCultureIgnoreCase))
End Function
Private Function GetPropertyValue(Of T)(propName As String) As T
Return DirectCast(Me.GetType().GetProperty(propName)?.GetValue(Me), T)
End Function
Private Function ContainsProperty(propName As String) As Boolean
Return Me.GetType().GetProperty(propName) IsNot Nothing
End Function
End Class
Upvotes: 0
Views: 51