Hi kana250688,
Please refer the below updated code.
Use the MaskedTextBoxInvoDate Leave event handler.
Code
VB.Net
Public Class Form1
Private OrderDetail As List(Of OrderDetail)
Private bindingSource As BindingSource = Nothing
Private _criteriasBindingList As New SortableBindingList(Of OrderDetail)()
Public Sub New()
InitializeComponent()
Me.CenterToScreen()
Dim CheckedBoxColumn As New DataGridViewCheckBoxColumn
CheckedBoxColumn.Width = 40
CheckedBoxColumn.Name = "checkboxcolumn"
CheckedBoxColumn.HeaderText = "Check"
CheckedBoxColumn.ReadOnly = False
DataGridView1.Columns.Insert(0, CheckedBoxColumn)
End Sub
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
},
New OrderDetail() With {
.Invono = "PI0005",
.InvoDate = Convert.ToDateTime("06-05-2024"),
.Days = Convert.ToDateTime("06-05-2024").ToString("dddd"),
.ProductName = "TEST 5000",
.UnitPrice = 19000,
.Quantity = 20
}
}
_criteriasBindingList = New SortableBindingList(Of OrderDetail)(OrderDetail)
bindingSource = New BindingSource With {.DataSource = _criteriasBindingList}
DataGridView1.DataSource = bindingSource
DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
End Sub
Private Sub myFilter(idPeople As String, InvoDate As String, days As String)
LoadData()
Dim data As IEnumerable(Of OrderDetail) = _criteriasBindingList
If Not String.IsNullOrEmpty(idPeople) Then
data = data.Where(Function(c) c.ProductName.ToLower().StartsWith(idPeople)).ToList()
End If
If Not String.IsNullOrEmpty(InvoDate) Then
Dim dt As Date
Dim isValid As Boolean = DateTime.TryParseExact(InvoDate, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, dt)
If isValid Then
data = data.Where(Function(c) c.InvoDate.ToString("dd-MM-yyyy").Equals(InvoDate)).ToList()
End If
End If
If Not String.IsNullOrEmpty(days) Then
data = data.Where(Function(c) c.Days.ToLower().StartsWith(days)).ToList()
End If
_criteriasBindingList = New SortableBindingList(Of OrderDetail)(CType(data, IList(Of OrderDetail)))
bindingSource.DataSource = _criteriasBindingList
End Sub
Private Sub TXT_FILTER(sender As Object, e As KeyEventArgs) Handles MaskedTextBoxInvoDate.KeyUp, TXTProductName.KeyUp, Txtdays.KeyUp
Dim ProductName = TXTProductName.Text.Trim().ToLower()
Dim InvoDate = MaskedTextBoxInvoDate.Text.Trim().ToLower()
Dim days = Txtdays.Text.Trim().ToLower()
myFilter(ProductName, InvoDate, days)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
_criteriasBindingList = New SortableBindingList(Of OrderDetail)(CType(bindingSource.DataSource, IList(Of OrderDetail)))
Dim i As Integer = 0
If _criteriasBindingList.Count > 0 Then
For Each row2 As DataGridViewRow In DataGridView1.Rows
Dim isselect As Boolean = Convert.ToBoolean(row2.Cells("checkboxcolumn").Value)
If isselect Then
Dim person As OrderDetail = _criteriasBindingList(row2.Index)
' Checking InvoDate in MaskedTextBox1.
'Error in below line
If MaskedTextBoxInvoDate.Text = " - -" Then
MaskedTextBoxInvoDate = Nothing
Else
If Not String.IsNullOrEmpty(MaskedTextBoxInvoDate.Text.Replace("-", "").Trim()) Then
_criteriasBindingList.Item(i).InvoDate = CDate(MaskedTextBoxInvoDate.Text)
End If
End If
' Checking ProductName in textbox1.
If Not String.IsNullOrEmpty(TextBox1.Text) Then
_criteriasBindingList.Item(i).ProductName = TextBox1.Text
End If
i = i + 1
End If
Next row2
End If
bindingSource = New BindingSource With {.DataSource = _criteriasBindingList}
DataGridView1.DataSource = bindingSource
MessageBox.Show("OrderDetail In successfully updated")
Catch ex As Exception
MessageBox.Show(ex.Message, "OrderDetail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
Private Sub MaskedTextBoxInvoDate_Leave(sender As Object, e As EventArgs) Handles MaskedTextBoxInvoDate.Leave
Dim dt As Date
Dim isValid As Boolean = DateTime.TryParseExact(MaskedTextBoxInvoDate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, dt)
If Not isValid AndAlso MaskedTextBoxInvoDate.Text <> " - -" Then
MaskedTextBoxInvoDate.Focus()
MessageBox.Show("Please Enter Valid date")
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
Screenshot