Reputation: 367
I'm Trying to use filter in the keyup event in Masktexbox running but the keyup event textbox is not running in vb.net
Is there something wrong with my code?
Please Guide me
Thanks
Imports System.Globalization
Public Class Form2
Private OrderDetail As List(Of OrderDetail)
Private bindingSource As BindingSource = Nothing
Private _criteriasBindingList As New SortableBindingList(Of OrderDetail)()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.LoadData()
End Sub
Private Sub LoadData()
OrderDetail = New List(Of OrderDetail) From {
New OrderDetail() With {
.Invono = "PI0001",
.InvoDate = Convert.ToDateTime("06-05-2024"),
.Days = Convert.ToDateTime("06-05-2024").ToString("dddd"),
.ProductName = "TEST 1000",
.UnitPrice = 15000,
.Quantity = 20
},
New OrderDetail() With {
.Invono = "PI0002",
.InvoDate = Convert.ToDateTime("07-05-2024"),
.Days = Convert.ToDateTime("07-05-2024").ToString("dddd"),
.ProductName = "TEST 2000",
.UnitPrice = 25000,
.Quantity = 20
},
New OrderDetail() With {
.Invono = "PI0003",
.InvoDate = Convert.ToDateTime("06-05-2024"),
.Days = Convert.ToDateTime("06-05-2024").ToString("dddd"),
.ProductName = "TEST 3000",
.UnitPrice = 17000,
.Quantity = 20
},
New OrderDetail() With {
.Invono = "PI0004",
.InvoDate = Convert.ToDateTime("07-05-2024"),
.Days = Convert.ToDateTime("07-05-2024").ToString("dddd"),
.ProductName = "TEST 4000",
.UnitPrice = 18000,
.Quantity = 20
}
}
_criteriasBindingList = New SortableBindingList(Of OrderDetail)(OrderDetail)
bindingSource = New BindingSource With {.DataSource = _criteriasBindingList}
DataGridView1.DataSource = bindingSource
End Sub
Private Sub myFilter(str1 As String, str2 As String, str3 As String)
LoadData()
If Not String.IsNullOrEmpty(str1) Then
bindingSource.DataSource = OrderDetail.Where(Function(c) c.InvoDate.ToString("dd-MM-yyyy").Contains(str2) AndAlso c.Days.ToLower.Contains(str3)).ToList()
ElseIf (String.IsNullOrEmpty(str2)) Then
bindingSource.DataSource = OrderDetail.Where(Function(c) c.ProductName.ToLower.Contains(str1) AndAlso c.Days.ToLower.Contains(str3)).ToList()
ElseIf (String.IsNullOrEmpty(str3)) Then
bindingSource.DataSource = OrderDetail.Where(Function(c) c.ProductName.ToLower.Contains(str1) AndAlso c.InvoDate.ToString("dd-MM-yyyy").Contains(str2)).ToList()
Else
bindingSource.DataSource = OrderDetail.Where(Function(c) c.ProductName.ToLower.Contains(str1) AndAlso c.InvoDate.ToString("dd-MM-yyyy").Equals(str2) AndAlso c.Days.ToLower.Contains(str3)).ToList()
End If
End Sub
Private Sub TXT_FILTER(sender As Object, e As KeyEventArgs) Handles MaskedTextBoxInvoDate.KeyUp, TXTProductName.KeyUp, Txtdays.KeyUp
Dim str1 = TXTProductName.Text.Trim().ToLower()
Dim str2 = MaskedTextBoxInvoDate.Text.Trim().ToLower()
Dim str3 = Txtdays.Text.Trim().ToLower()
Dim dt As Date
Dim isValid As Boolean = DateTime.TryParseExact(str2, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, dt)
If isValid Then
myFilter(str1, str2, str3)
Else
myFilter(String.Empty, String.Empty, String.Empty)
End If
End Sub
End Class
Public Class OrderDetail
Public Property Invono As String
Public Property InvoDate As DateTime
Public Property Days As String
Public Property ProductName As String
Public Property UnitPrice As Integer
Public Property Quantity As Integer
End Class
Code in Class SortableBindingList
`Imports System Imports System.Collections.Generic Imports System.ComponentModel
''' ''' Provides a generic collection that supports data binding and additionally supports sorting. ''' See http://msdn.microsoft.com/en-us/library/ms993236.aspx ''' If the elements are IComparable it uses that; otherwise compares the ToString() ''' ''' The type of elements in the list. Public Class SortableBindingList(Of T As Class) Inherits BindingList(Of T)
Private _isSorted As Boolean
Private _sortDirection As ListSortDirection = ListSortDirection.Ascending
Private _sortProperty As PropertyDescriptor
''' <summary>
''' Initializes a new instance of the <see cref="SortableBindingList{T}"/> class.
''' </summary>
Public Sub New()
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="SortableBindingList{T}"/> class.
''' </summary>
''' <param name="list">An <see cref="T:System.Collections.Generic.IList`1" /> of items to be contained in the <see cref="T:System.ComponentModel.BindingList`1" />.</param>
Public Sub New(ByVal list As IList(Of T))
MyBase.New(list)
End Sub
''' <summary>
''' Gets a value indicating whether the list supports sorting.
''' </summary>
Protected Overrides ReadOnly Property SupportsSortingCore As Boolean
Get
Return True
End Get
End Property
''' <summary>
''' Gets a value indicating whether the list is sorted.
''' </summary>
Protected Overrides ReadOnly Property IsSortedCore As Boolean
Get
Return _isSorted
End Get
End Property
''' <summary>
''' Gets the direction the list is sorted.
''' </summary>
Protected Overrides ReadOnly Property SortDirectionCore As ListSortDirection
Get
Return _sortDirection
End Get
End Property
''' <summary>
''' Gets the property descriptor that is used for sorting the list if sorting is implemented in a derived class; otherwise, returns null
''' </summary>
Protected Overrides ReadOnly Property SortPropertyCore As PropertyDescriptor
Get
Return _sortProperty
End Get
End Property
''' <summary>
''' Removes any sort applied with ApplySortCore if sorting is implemented
''' </summary>
Protected Overrides Sub RemoveSortCore()
_sortDirection = ListSortDirection.Ascending
_sortProperty = Nothing
_isSorted = False 'thanks Luca
End Sub
''' <summary>
''' Sorts the items if overridden in a derived class
''' </summary>
''' <param name="prop"></param>
''' <param name="direction"></param>
Protected Overrides Sub ApplySortCore(ByVal prop As PropertyDescriptor, ByVal direction As ListSortDirection)
_sortProperty = prop
_sortDirection = direction
Dim list As List(Of T) = TryCast(Items, List(Of T))
If list Is Nothing Then
Return
End If
list.Sort(AddressOf Compare)
_isSorted = True
'fire an event that the list has been changed.
OnListChanged(New ListChangedEventArgs(ListChangedType.Reset, -1))
End Sub
Private Function Compare(ByVal lhs As T, ByVal rhs As T) As Integer
Dim result = OnComparison(lhs, rhs)
'invert if descending
If _sortDirection = ListSortDirection.Descending Then
result = -result
End If
Return result
End Function
Private Function OnComparison(ByVal lhs As T, ByVal rhs As T) As Integer
Dim lhsValue As Object = If(lhs Is Nothing, Nothing, _sortProperty.GetValue(lhs))
Dim rhsValue As Object = If(rhs Is Nothing, Nothing, _sortProperty.GetValue(rhs))
If lhsValue Is Nothing Then
Return If(rhsValue Is Nothing, 0, -1) 'nulls are equal
End If
If rhsValue Is Nothing Then
Return 1 'first has value, second doesn't
End If
If TypeOf lhsValue Is IComparable Then
Return DirectCast(lhsValue, IComparable).CompareTo(rhsValue)
End If
If lhsValue.Equals(rhsValue) Then
Return 0 'both are the same
End If
'not comparable, compare ToString
Return lhsValue.ToString().CompareTo(rhsValue.ToString())
End Function
End Class
`
Upvotes: 0
Views: 52