Filter DataGridView by Interval Between Dates in VB.NET

1

I'm new to programming and I'm finishing a job, but I have a problem that I've been experiencing for weeks and I have not been able to solve it.

I created a simple system that registers the OSs (work orders), and in a form I put a datagrid that pulls all the OS.

I made a filter that is to search the employees that I type in the text box, but it searches for all the services made by the name entered, so I need to know how to make a second filter, where I will inform the start date and the end date. / p>

Example: I need to know what the employee did, then I put the employee's name in the box and I make the filter, and the services appear in datagrid , but I need to know how to filter another date only, the dates in another field with a range. ex: 10/10/2014 until 10/20/2014. I tried in some ways but I did not get it at all.

Could someone explain me, please? I am using VB.NET and Access 2003.

Code:

Dim cn As New OleDb.OleDbConnection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Firebird.mdb"
cn.Open()

Dim dt1 As DateTime = DateTimePicker1.Value.Date
Dim dt2 As DateTime = DateTimePicker2.Value.Date
'MsgBox(dt1)
Try

    MsgBox(dt1 + " " + dt2)

    Dim cmd As OleDbCommand
    Dim dt As DataTable
    Dim Da As OleDbDataAdapter
    With Cmd
        .CommandType = CommandType.Text
        .CommandText = "select * FROM OS where data between @datainicial AND @datafinal"
        .Parameters.AddWithValue("@datainicial", dt1)
        .Parameters.AddWithValue("@datafinal", dt2)
        .Connection = cn
    End With

    With OSTableAdapter.Adapter
        .SelectCommand = cmd
        dt = New DataTable
        .Fill(dt)
        OSDataGridView.DataSource = dt
    End With

    cn.Close()
Catch ex As Exception
    MsgBox(ex.Message)
End Try
    
asked by anonymous 03.10.2014 / 20:25

1 answer

0

You do not have to pass dates as a parameter. You can use the RowFilter property of your DataGridView as follows:

DirectCast(OSDataGridView.DataSource, DataTable).DefaultView.RowFilter = "data > #" & DateTimePicker1.Value & "# and data < #" & DateTimePicker2.Value & "#"
    
03.10.2014 / 20:57