Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the code I have now:

Dim startYear As Integer, endYear As Integer
        If CheckBox1.Checked = True Then
            If Integer.TryParse(startyear.Text, startYear) AndAlso Integer.TryParse(endyear.Text, endYear) Then
                datat.DefaultView.RowFilter = "[startofyear] >=  '" & startyear.Text & "' and [endofyear] <= '" & endyear.Text & "'"

            End If
        End If


This filters out by the year, showing the result between both textbox years. However, I am looking to do an exclude function which will start at startyear and end at endyear, but gets all values in between. For instance my year range might be 2000-2005 I would want to exclude from the data set. So, 2000, 2001, 2002, 2003, 2005, and 2005 all would be exlcuded.

What I have tried:

I tried to use not in my statement. for instance,
datat.DefaultView.RowFilter = "not [startofyear] >=  '" & startyear.Text & "' and [endofyear] <= '" & endyear.Text & "'"


I have also tried
datat.DefaultView.RowFilter = "[startofyear] <>  '" & startyear.Text & "' and [endofyear] <> '" & endyear.Text & "'"


this seemed to almost exclude everything, so I know that its wrong based on the data filtered and displayed in datagridview.
Posted
Updated 10-Apr-18 8:01am
Comments
Kornfeld Eliyahu Peter 10-Apr-18 10:52am    
It should work just like SQL, so you can try BETWEEN...
But if you want to include lower bound but exclude upper you should use this formula:
lower <= vale < upper

1 solution

Quote:
I would want to exclude from the data set. So, 2000, 2001, 2002, 2003, 2005, and 2005 all would be exlcuded.


It's quite simple... Depending on situation, you may use:
VB
datat.DefaultView.RowFilter = String.Format("([startofyear] NOT BETWEEN {0} AND {1}) OR ([endofyear] NOT BETWEEN {0} AND {1})", startyear.Text, endyear.Text)
'or
datat.DefaultView.RowFilter = String.Format("([endofyear] < {0} OR [startofyear] > {1})", startyear.Text, endyear.Text)


For further details, please see:
DataView.RowFilter Property (System.Data)[^]
DataColumn.Expression Property (System.Data)[^]

[EDIT]
I almost forgot...
To help you understand date overlapping, read this: Time Period Library for .NET[^]
 
Share this answer
 
v2
Comments
Member 11856456 10-Apr-18 15:01pm    
I am getting this error: "The expression contains unsupported operator 'Between'.'"
Maciej Los 10-Apr-18 15:18pm    
Strange...
Between is proper expression. So, try second solution.
Member 11856456 10-Apr-18 15:44pm    
The first one didn't work. however, the second solution worked perfectly. I am a little confused on how it works. Can you please explain to me how this solution works so I can possibly manipulate the solution if I need to. Originally I tried to use the and statement to say in between this and that exclude these, but that didn't work. So I am so interested on why this OR statement works the way I need it to?
Maciej Los 10-Apr-18 15:49pm    
Glad, i can help. For details, follow the link i added at the end of my answer ;)
Cheers,
Maciej
Maciej Los 11-Apr-18 2:30am    
Think of it... Test below code to find out why OR is working fine and AND operator does not:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("startofyear", typeof(int)));
dt.Columns.Add(new DataColumn("endofyear", typeof(int)));

Random rs = new Random();
Random re = new Random();
dt = Enumerable.Range(0, 100)
	.Select(x=> dt.LoadDataRow(new object[]{rs.Next(1995, 2010), rs.Next(1995, 2010) + re.Next(1, 11)}, false))
	.CopyToDataTable();
	
int[] yearsToExclude = {2000,2005};

var result = dt.AsEnumerable()
	.Select(x=> new
	{
		startofyear	= x.Field<int>("startofyear"),
		endofyear = x.Field<int>("endofyear"),
		And_Operator = x.Field<int>("endofyear") < yearsToExclude[0] &&  x.Field<int>("startofyear") > yearsToExclude[1], 
		Or_Operator = x.Field<int>("endofyear") < yearsToExclude[0] ||  x.Field<int>("startofyear") > yearsToExclude[1]
	})
	.ToList();


Cheers,
Maciej

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900