Hi kana250688,
I have created a sample. Refer this sample and modify as per your class and code.
Form Design
The form consists of a MaskedTextBox and DataGridView controls.
The MaskedTextBox has been set with Custom Input Mask and the KeyUp event handler has been assigned.
Namespaces
C#
using System.Globalization;
VB.Net
Imports System.Globalization
Property Class
C#
public class OrderDetail
{
public string Invono { get; set; }
public DateTime InvoDate { get; set; }
public string ProductName { get; set; }
public int UnitPrice { get; set; }
public int Quantity { get; set; }
}
VB.Net
Public Class OrderDetail
Public Property Invono As String
Public Property InvoDate As DateTime
Public Property ProductName As String
Public Property UnitPrice As Integer
Public Property Quantity As Integer
End Class
Code
The records are filtered using the myFilter method by passing the MaskedTextBox value on KeyUp event.
If the MaskedTextBox value has invalid Date then all the records are displayed else the filter records are displayed.
C#
private List<OrderDetail> OrderDetail;
private BindingSource bindingSource = null;
private void Form1_Load(object sender, EventArgs e)
{
this.LoadData();
}
private void LoadData()
{
OrderDetail = new List<OrderDetail>()
{
new OrderDetail()
{
Invono = "PI0001",
InvoDate =Convert.ToDateTime( "06-05-2024"),
ProductName = "TEST 1000",
UnitPrice = 15000,
Quantity = 20
},
new OrderDetail()
{
Invono = "PI0002",
InvoDate = Convert.ToDateTime("07-05-2024"),
ProductName = "TEST 2000",
UnitPrice = 25000,
Quantity = 20
},
new OrderDetail()
{
Invono = "PI0003",
InvoDate = Convert.ToDateTime("06-05-2024"),
ProductName = "TEST 3000",
UnitPrice = 17000,
Quantity = 20
},
new OrderDetail()
{
Invono = "PI0004",
InvoDate = Convert.ToDateTime("07-05-2024"),
ProductName = "TEST 4000",
UnitPrice = 18000,
Quantity = 20
}
};
bindingSource = new BindingSource() { DataSource = OrderDetail };
DataGridView1.DataSource = bindingSource;
}
private void myFilter(string str2)
{
if (!string.IsNullOrEmpty(str2))
{
bindingSource.DataSource = OrderDetail.Where(c => c.InvoDate.ToString("dd-MM-yyyy").Equals(str2)).ToList();
}
else
{
bindingSource.DataSource = OrderDetail;
}
DataGridView1.DataSource = bindingSource;
}
private void MaskedTextBox1_KeyUp(object sender, KeyEventArgs e)
{
var str2 = MaskedTextBox1.Text.Trim().ToLower();
DateTime dt;
bool isValid = DateTime.TryParseExact(str2, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
if (isValid)
{
myFilter(str2);
}
else
{
myFilter(string.Empty);
}
}
VB.Net
Private OrderDetail As List(Of OrderDetail)
Private bindingSource As BindingSource = Nothing
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"),
.ProductName = "TEST 1000",
.UnitPrice = 15000,
.Quantity = 20
},
New OrderDetail() With {
.Invono = "PI0002",
.InvoDate = Convert.ToDateTime("07-05-2024"),
.ProductName = "TEST 2000",
.UnitPrice = 25000,
.Quantity = 20
},
New OrderDetail() With {
.Invono = "PI0003",
.InvoDate = Convert.ToDateTime("06-05-2024"),
.ProductName = "TEST 3000",
.UnitPrice = 17000,
.Quantity = 20
},
New OrderDetail() With {
.Invono = "PI0004",
.InvoDate = Convert.ToDateTime("07-05-2024"),
.ProductName = "TEST 4000",
.UnitPrice = 18000,
.Quantity = 20
}
}
bindingSource = New BindingSource With {.DataSource = OrderDetail}
DataGridView1.DataSource = bindingSource
End Sub
Private Sub myFilter(str2 As String)
If Not String.IsNullOrEmpty(str2) Then
bindingSource.DataSource = OrderDetail.Where(Function(c) c.InvoDate.ToString("dd-MM-yyyy").Equals(str2)).ToList()
Else
bindingSource.DataSource = OrderDetail
End If
DataGridView1.DataSource = bindingSource
End Sub
Private Sub MaskedTextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles MaskedTextBox1.KeyUp
Dim str2 = MaskedTextBox1.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(str2)
Else
myFilter(String.Empty)
End If
End Sub
Screenshot