Reputation: 1835
I am trying to write some code that would write filter based off multiple conditions. In my app I have 2 date filters and 2 text filters. 1. Is it possible to filter multiple conditions using the binding source filter property?
If not that how would I go about doing that?
My code below
private void btnFilter_Click(object sender, EventArgs e)
{
string strFilter = "";
string strDateFilter = "Date >" + String.Format("{0:MM/dd/yyyy}", dtBgnDate.Value) + " AND Date < " + String.Format("{0:MM/dd/yyyy}", dtEndDate.Value);
string strNameFilter;
string strAcctFilter;
strFilter = strDateFilter;
if (!txtLName.Text.Trim().Equals(""))
{
strNameFilter = "LName like '%" + txtLName.Text + "%'";
strFilter = strFilter + " AND " + strNameFilter;
}
if (!txtAcctNum.Text.Trim().Equals(""))
{
strAcctFilter = "AcctNum = " + txtAcctNum.Text;
strFilter = strFilter + " AND " + strAcctFilter;
}
BindingSource bsource = new BindingSource();
bsource.Filter = strFilter;
bsource.DataSource = gvData.DataSource;
}
The code throws the following exception
EvaluatedException: Cannot perform '>' operation on System.DateTime and Double
Upvotes: 1
Views: 956
Reputation: 8879
When filtering date values, you have to enclose them with sharp characters i.e. #12/31/2008#
Thus, change the following:
string strDateFilter = "Date > #" + String.Format("{0:MM/dd/yyyy}", dtBgnDate.Value) + "# AND Date < #" + String.Format("{0:MM/dd/yyyy}", dtEndDate.Value) + "#";
Upvotes: 1