|
-
Jul 30th, 2019, 08:44 AM
#1
Thread Starter
Lively Member
[RESOLVED] Filter Rows by Date
Dear all,
I'm in trouble for a datagridview where I would like to filter rows by the column Date.
In particular I have a textbox in which I insert the Year value; then I'm building up two Strings using 01/01/+Year and 31/12/+Year and finally I would like to filter out the rows for which the Date value is outside the defined range.
Here below the code:
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
On Error GoTo SearchErr
Dim date1 As String = "01/01/" & TextBox1.Text.Trim
Dim date2 As String = "31/12/" & TextBox1.Text.Trim
Dim dt1 As DateTime = DateTime.Parse(date1)
Dim dt2 As DateTime = DateTime.Parse(date2)
If TextBox1.Text = "" And TextBox2.Text = "" And IsNothing(CStr(ComboBox1.SelectedItem)) Then
Exit Sub
ElseIf TextBox1.Text = "" And TextBox2.Text <> "" And IsNothing(CStr(ComboBox1.SelectedItem)) Then
dtTableGrd.DefaultView.RowFilter = "Acc Like '%" & TextBox2.Text & "%'"
ElseIf TextBox1.Text = "" And TextBox2.Text = "" And Not IsNothing(CStr(ComboBox1.SelectedItem)) Then
dtTableGrd.DefaultView.RowFilter = "Leva Like '%" & ComboBox1.SelectedItem.ToString & "%'"
ElseIf TextBox1.Text <> "" And TextBox2.Text = "" And IsNothing(CStr(ComboBox1.SelectedItem)) Then
dtTableGrd.DefaultView.RowFilter = "Data >= #" + dt1 + "# AND Data <= #" + dt2 + "#"
End If
ErrEx:
Exit Sub
SearchErr:
MsgBox("Errore numero:" & Err.Number & vbNewLine & "Descrizione errore:" & Err.Description, MsgBoxStyle.Critical, "Errore Ricerca!")
Resume ErrEx
End Sub
The code doesn't work and, debugging, the message I get is: "String was not recognized as a valid DateTime".
Any suggestion? I guess I'm converting badly the Strings, and I did a lot of tentative but without any success...
Thanks,
A.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|