Dear Sir,
I'm Trying update to reflect to datagridview but I have an error "Conversion from string " - -" to type `Date` is not valid" in maskedTextBox
and how to validate date in maskedTextBox so that the user does not type outside the MaskTextBox format
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)()
Public Sub New()
InitializeComponent()
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
}
}
_criteriasBindingList = New SortableBindingList(Of OrderDetail)(OrderDetail)
bindingSource = New BindingSource With {.DataSource = _criteriasBindingList}
DataGridView1.DataSource = bindingSource
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 MaskedTextBox1.Text = " - -" Then
MaskedTextBox1 = Nothing
Else
If Not String.IsNullOrEmpty(MaskedTextBox1.Text) Then
_criteriasBindingList.Item(i).InvoDate = CDate(MaskedTextBox1.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
End Class