• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 747
  • Last Modified:

Filter datagridview by range.

How do I filter a datagridview for a date range?
I have a table that I'm displaying in a datagridview.  I want to be able to click a button and ask the user to enter dates to filter the datagridview by.   One of the fields in the datagridview is obviously a date.  Once the two dates are entered, I want the datagridview to only show records from the 1st date, to the 2nd date.

0
dreinmann
Asked:
dreinmann
2 Solutions
 
Bob LearnedCommented:
If your DataGridView is bound to a DataTable, then you can use the DefaultView (which is a DataView) to filter the records.

Example:

Dim dt As DataTable = Me.DataSet1.Tables(0)
dt.DefaultView = "CurrentDate >= '2007-10-1' AND CurrentDate <= '2007-10-18' "
Me.DataGridView1.DataSource = dt.DefaultView

Bob
0
 
JimBrandleyCommented:
Loop over the set of rows in the collection.
1. If you want to hide them, test the date for that row. If outside the range, set row.Visible to false.

2. If you want to remove them, start at the last row, and work your way to the first. If the date is outside the range, yourDataGridView.Rows.RemoveAt(currentIndex)

Jim
0
 
VBRocksCommented:
This really is not much different than the approach offered by TheLearnedOne, except that this example
uses the source of the DataGridView, and accomplish it (basically) in 1 line of code, and he didn't use
the "#" when filtering, which will cause an error.

        'Set these to the dates entered by the user
        Dim startDate As Date = Today.Date
        Dim enddate As Date = DateAdd(DateInterval.Day, 2, Today.Date)

        'Assuming your column name in your table is "Date"
        DirectCast(Me.DataGridView1.DataSource, DataTable).DefaultView.RowFilter = _
            "Date>=#" & startDate & "# AND Date<=#" & enddate & "#"



0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
VBRocksCommented:
My apologies to TheLearnedOne for referencing you.  I have the utmost respect for you.  I just didn't
want it to appear as though I was duplicating your code, without showing what I was doing different.

0
 
dreinmannAuthor Commented:
dt.DefaultView keeps coming up with an error saying that it is readonly.  Readonly is fine with me, but the program won't allow it.
0
 
VBRocksCommented:
INTERESTING...  How do you end up with a ReadOnly DataGridView?

Try this alternate approach, it should work for your:

        'Set these to the dates entered by the user
        Dim startDate As Date = Today.Date
        Dim enddate As Date = DateAdd(DateInterval.Day, 2, Today.Date)

        'Assuming your column name in your table is "Date"
        Dim bs As New BindingSource()
        bs.DataSource = Me.DataGridView1.DataSource
        bs.Filter = "Date>=#" & startDate & "# AND Date<=#" & enddate & "#"
        Me.DataGridView1.DataSource = bs

0
 
dreinmannAuthor Commented:
This worked:
Dim dt As DataTable = ds.Tables("TimeOffRequests")
        dt.DefaultView.RowFilter = "StartDateTime >= #10/24/2007# AND StartDateTime <= #10/25/2007#"
        Me.dgvViewRequests.DataSource = dt.DefaultView

I was missing the .RowFilter at the end of dt.DefaultView.
The dates work as '10/24/2007' or as #10/24/2007#.

thanks all
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now