|
-
Oct 25th, 2013, 04:52 AM
#1
Thread Starter
Frenzied Member
[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")
-
Oct 25th, 2013, 05:04 AM
#2
Re: Filter DatagridView By Month
try this:
Code:
payments.PaymentsBindingSource.Filter ="Convert(Date , 'System.String') LIKE '*july*'"
__________________
Rate the posts that helped you 
-
Oct 25th, 2013, 05:09 AM
#3
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.
-
Oct 25th, 2013, 05:10 AM
#4
Re: Filter DatagridView By Month
 Originally Posted by riteshjain1982
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.
-
Oct 25th, 2013, 05:37 AM
#5
Re: Filter DatagridView By Month
 Originally Posted by jmcilhinney
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 
-
Oct 25th, 2013, 05:49 AM
#6
Thread Starter
Frenzied Member
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
-
Oct 25th, 2013, 06:37 AM
#7
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.
-
Oct 25th, 2013, 04:08 PM
#8
Thread Starter
Frenzied Member
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.
-
Oct 26th, 2013, 04:05 AM
#9
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.
-
Oct 26th, 2013, 04:06 PM
#10
Thread Starter
Frenzied Member
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
-
Oct 26th, 2013, 05:12 PM
#11
Thread Starter
Frenzied Member
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
-
Oct 26th, 2013, 05:31 PM
#12
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|