DataTable BindingSource到DateGridView过滤

时间:2013-12-17 22:05:26

标签: c# datagridview bindingsource

我正在尝试编写一些可以根据多个条件编写过滤器的代码。在我的应用程序中,我有2个日期过滤器和2个文本过滤器 1.是否可以使用绑定源过滤器属性过滤多个条件?

如果不是,我该怎么做呢?

我的代码

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;

}

代码抛出以下异常

EvaluatedException: Cannot perform '>' operation on System.DateTime and Double

1 个答案:

答案 0 :(得分:1)

过滤日期值时,您必须用明确的字符(#12/31/2008#

)将它们括起来

因此,请更改以下内容:

string strDateFilter = "Date > #" + String.Format("{0:MM/dd/yyyy}", dtBgnDate.Value) + "# AND Date < #" + String.Format("{0:MM/dd/yyyy}", dtEndDate.Value) + "#";