过滤多个参数

时间:2014-07-24 04:21:04

标签: asp.net crystal-reports reporting

我想通过组合框过滤水晶报表中的多个参数,但问题是一次有一个过滤器处于活动状态。

以下是两个组合框的索引更改代码:

protected void drpUserName_SelectedIndexChanged(object sender, EventArgs e)
        {
            username = drpUserName.SelectedValue;

            ReportDocument rd = new ReportDocument();
            rd.Load(Server.MapPath("LeaveReport.rpt"));

            rd.SetParameterValue("username", username);
            rd.SetParameterValue("status", status);

            rd.SetDatabaseLogon("cde_portal", "credyna", "SERVER\\SQLEXPRESS", "lbs");

        CrystalReportViewer1.ReportSource = rd;

    }

    protected void drpStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        status = drpStatus.SelectedValue;

        ReportDocument rd = new ReportDocument();
        rd.Load(Server.MapPath("LeaveReport.rpt"));
        rd.SetParameterValue("status", status);
        rd.SetParameterValue("username", username);

        rd.SetDatabaseLogon("cde_portal", "credyna", "SERVER\\SQLEXPRESS", "lbs");

        CrystalReportViewer1.ReportSource = rd;

    }

这是记录选择公式:

 If  {?username} = "-1" Then                     // -1 for all values
 {tblEmployee.Employeer_UserName} <> {?username} // return all records
 Else
 {tblEmployee.Employeer_UserName} = {?username} // return selected records
 and IF {?status} = "-1" Then
 {tblLeave.leave_status} <> {?status}
 Else
 {tblLeave.leave_status} = {?status}

上述公式中还有一件事:如果我首先检查{?username},然后在这种情况下检查{?status} {?username}过滤是否正常,但如果我先检查{?status}那么{?status},过滤效果很好。

1 个答案:

答案 0 :(得分:1)

你可以这样做......

protected void drpUserName_SelectedIndexChanged(object sender, EventArgs e)
        {

            string strSelection = "1=1";
            rd.SetParameterValue("username", drpUserName.SelectedValue);
            rd.SetParameterValue("status", drpStatus.SelectedValue);

            if (drpUserName.SelectedValue != "-1")
            {
                strSelection = strSelection + "And {tblEmployee.Employeer_UserName}=" + "\""+ drpUserName.SelectedValue+"\"";
            }
            if ( drpStatus.SelectedValue != "-1")
            {
                strSelection = strSelection + "And {tblLeave.leave_status}=" +"\""+ drpStatus.SelectedValue+"\"" ;
            }
            rd.RecordSelectionFormula = strSelection;
            rd.SetDatabaseLogon("cde_portal", "credyna", "SERVER\\SQLEXPRESS", "lbs");
            CrystalReportViewer1.ReportSource = rd;

        }

        protected void drpStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strSelection = "1=1";
            rd.SetParameterValue("username", drpUserName.SelectedValue);
            rd.SetParameterValue("status", drpStatus.SelectedValue);

            if (drpUserName.SelectedValue != "-1")
            {
                strSelection = strSelection + "And {tblEmployee.Employeer_UserName}=" + "\"" + drpUserName.SelectedValue + "\"";
            }
            if (drpStatus.SelectedValue != "-1")
            {
                strSelection = strSelection + "And {tblLeave.leave_status}=" + "\"" + drpStatus.SelectedValue + "\"";
            }
            rd.RecordSelectionFormula = strSelection;
            rd.SetDatabaseLogon("cde_portal", "credyna", "SERVER\\SQLEXPRESS", "lbs");
            CrystalReportViewer1.ReportSource = rd;

        }