[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")
Re: Filter DatagridView By Month
try this:
Code:
payments.PaymentsBindingSource.Filter ="Convert(Date , 'System.String') LIKE '*july*'"
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.
Re: Filter DatagridView By Month
Quote:
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.
Re: Filter DatagridView By Month
Quote:
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/*'"
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))
Quote:
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
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.
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
http://img27.imageshack.us/img27/4571/t1il.jpg
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.
Quote:
Input string was not in a correct format.
Re: Filter DatagridView By Month
I said:
Quote:
Also, if the column name actually is Date, you might have to wrap it in brackets.
You've used braces, not brackets.
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
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
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