Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi friends!
I have a problem when I search in datagridview with DateTimePicker1, DateTimePicker2, Textbox1.
DateTimePicker1 : DATE_FROM
DateTimePicker2 : DATE_TO
Textbox1 : NUMBER_RECEIPT

I choose DateTimePicker1 from 01/01/2017 and DateTimePicker2 to 25/09/2017 and show:

| NUMBER_RECEIPT | DATE | DESCRIPTIONS |
| 12 | 12/09/2017 | Item 14 |
| 12 | 12/09/2017 | Item 15 |
| 13 | 17/09/2017 | Item 23 |
| 14 | 25/09/2017 | Item 31 |

When I choose DateTimePicker1 from 17/09/2017 and DateTimePicker2 to 25/09/2017 show:

| NUMBER_RECEIPT | DATE | DESCRIPTIONS |
| 13 | 17/09/2017 | Item 23 |
| 14 | 25/09/2017 | Item 31 |

My problem showed when I search with Textbox1 after choose date from 17/09/2017 to 25/09/2017: 12

| NUMBER_RECEIPT | DATE | DESCRIPTIONS |
| 12 | 12/09/2017 | Item 14 |
| 12 | 12/09/2017 | Item 15 |

NUMBER_RECEIPT 12 is not from 17/09/2017 to 25/09/2017 .

What I have tried:

I tried different way but without result.

VB
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    Select Case e.KeyCode
        Case Keys.Enter
        BindingSource18.Filter = "[NUMBER_RECEIPT] LIKE '" & TextBox1.Text & "%'"
    End Select
End Sub


VB
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        Me.BindingSource18.Filter = "DATE_FROM >= '" & DateTimePicker1.Text & "' and DATE_TO <= '" & DateTimePicker2.Text & "'"
End Sub


VB
Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged
        Me.BindingSource18.Filter = "DATE_FROM >= '" & DateTimePicker1.Text & "' and DATE_TO <= '" & DateTimePicker2.Text & "'"
End Sub


Please can you help me? THANKS :)
Posted
Updated 27-Sep-17 4:54am

1 solution

Another variation on the same theme! :)
VB.NET
Private Sub UpdateFilter()
    Dim parts As New List(Of String)()
    
    If Not String.IsNullOrWhiteSpace(TextBox1.Text) Then
        parts.Add("[NUMBER_RECEIPT] LIKE '" & TextBox1.Text & "%'")
    End If
    If DateTimePicker1.Checked Then
        parts.Add(string.Format("[DATE_FROM] >= #{0:yyyy/MM/dd}#", DateTimePicker1.Value))
    End If
    If DateTimePicker2.Checked Then
        parts.Add(string.Format("[DATE_FROM] <= #{0:yyyy/MM/dd}#", DateTimePicker2.Value))
    End If
    
    If parts.Count = 0 Then
        BindingSource18.Filter = ""
    Else
        BindingSource18.Filter = String.Join(" AND ", parts)
    End If
End Sub

Private Sub FilterChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.KeyDown, DateTimePicker1.ValueChanged, DateTimePicker2.ValueChanged
    UpdateFilter()
End Sub
 
Share this answer
 
Comments
ionMEMBER 27-Sep-17 10:59am    
Thank you very much!! you helped me a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900