Results 1 to 12 of 12

Thread: [RESOLVED] Filter DatagridView By Month

  1. #1

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Resolved [RESOLVED] Filter DatagridView By Month

    I am unable to find info on Filter a DatagridView by month only.

    There are many examples including MSDN but none seem to be related to month only.

    One of many i have been trying is below:
    Code:
        payments.PaymentsBindingSource.Filter = String.Format("Date = #{0:M/dd/yyyy}#", "july")

  2. #2
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Filter DatagridView By Month

    try this:
    Code:
    payments.PaymentsBindingSource.Filter ="Convert(Date , 'System.String') LIKE '*july*'"
    __________________
    Rate the posts that helped you

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Filter DatagridView By Month

    Um, how exactly do you expect the string "july" to be formatted as a date? You can't filter by month explicitly unless you actually have a column that contains just the month. If you have a column that contains dates then you either have to provide a date filter that will produce the results you want or you have to extract the month from the date and compare that. Assuming that you want July of a particular year:
    Code:
    Dim myDate As New Date(myYear, 7, 1)
    
    payments.PaymentsBindingSource.Filter = String.Format("Date >= #{0:M/dd/yyyy}# AND < {1:M/dd/yyyy}", myDate, myDate.AddMonths(1))
    If you want July of any year then you'll have to take the second option, i.e. extract the month out of each date and see if it is 7.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Filter DatagridView By Month

    Quote Originally Posted by riteshjain1982 View Post
    try this:
    Code:
    payments.PaymentsBindingSource.Filter ="Convert(Date , 'System.String') LIKE '*july*'"
    Will converting a date value to a string result in the full month name being included? I don't know for sure but I'm guessing not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Filter DatagridView By Month

    Quote Originally Posted by jmcilhinney View Post
    Will converting a date value to a string result in the full month name being included? I don't know for sure but I'm guessing not.
    I just overlook that....you are right, it will not going to convert it into string with month name...

    I think code can be modified to something like this
    Code:
    payments.PaymentsBindingSource.Filter ="Convert(Date , 'System.String') LIKE '7/*'"
    __________________
    Rate the posts that helped you

  6. #6

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Filter DatagridView By Month

    Yes a particular year.

    I am getting this error

    Code:
      Dim myDate As New Date(2013, 7, 1)
    
            payments.PaymentsBindingSource.Filter = String.Format("Date >= #{0:M/dd/yyyy}# AND < {1:M/dd/yyyy}", myDate, myDate.AddMonths(1))

    Syntax error: Missing operand before '<' operator.
    Had a google on what a operand is and cannot work it out.
    http://www.tutorialspoint.com/vb.net..._operators.htm

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Filter DatagridView By Month

    You need to specify the column name for the second condition as well, which I omitted by mistake. Also, if the column name actually is Date, you might have to wrap it in brackets. Not sure whether it's a keyword in that context or not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Filter DatagridView By Month

    second colum name is the same "date"
    i am trying to filter the DGV when i click the "Jul" button in the menustrip




    Code:
    Dim myDate As New Date(2013, 7, 1)
    
            payments.PaymentsBindingSource1.Filter = String.Format("{date} >= #{0:M/dd/yyyy}# AND {date} <{1:M/dd/yyyy}", myDate, myDate.AddMonths(1))
    now is raises this error.

    Input string was not in a correct format.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Filter DatagridView By Month

    I said:
    Also, if the column name actually is Date, you might have to wrap it in brackets.
    You've used braces, not brackets.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Filter DatagridView By Month

    I did try brackets.

    Code:
          Dim myDate As New Date(2013, 7, 1)
    
            payments.PaymentsBindingSource1.Filter = String.Format("date >= #{0:M/dd/yyyy}# AND date <#{1:M/dd/yyyy}#", myDate, myDate.AddMonths(1))
    It required "#" around the second date format, also tried with and without brackets and it works on both occasions.

    Thanks heaps
    toe

  11. #11

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: [RESOLVED] Filter DatagridView By Month

    Ok,

    It runs but doesnt work with the financial year when spanning different years eg; jul 2012 to jun 2013

    I have tried a few variation unsuccessfully

    Code:
        Public Sub fFilterMonthPayments()
            'http://www.vbforums.com/newreply.php?do=postreply&t=739975
            Dim myDate As New Date(2012, mMonth, 1)
            Dim myDate2 As New Date(2013, mMonth, 1)
            payments.PaymentsBindingSource1.Filter = String.Format("date >= #{0:M/dd/yyyy}# AND date <#{1:M/dd/yyyy}#", myDate, myDate2.AddMonths(1))
        End Sub

  12. #12

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Smile Re: [RESOLVED] Filter DatagridView By Month

    Code:
       Private Sub DecToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DecToolStripMenuItem2.Click
            yYear = 2012
            mMonth = 12
            fFilterMonthPayments()
        End Sub
    
        Private Sub JanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JanToolStripMenuItem.Click
            yYear = 2013
            mMonth = 1
            fFilterMonthPayments()
        End Sub

    Code:
        Public Sub fFilterMonthPayments()
            'http://www.vbforums.com/newreply.php?do=postreply&t=739975
            Dim myDate As New Date(yYear, mMonth, 1)
            payments.PaymentsBindingSource1.Filter = String.Format("date >= #{0:M/dd/yyyy}# AND date <#{1:M/dd/yyyy}#", myDate, myDate.AddMonths(1))
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width