使用多个复选框过滤Gridview结果(如何使和/或语句工作)

时间:2012-07-05 21:37:29

标签: c# asp.net gridview filter sqldatasource

所以,我有GridView," CustomerGridView,"和SqlDataSource," SqlDataSource1,"从我的sql表中提取数据。现在我想添加一些过滤,所以我创建了一个带有各种选项的侧面板。

我的问题是,根据我目前的代码,如果我选择,请说每月支付的客户,新客户以及每半年支付一次的客户。它向我展示了所有这些类型,包括符合标准的活跃(非新)客户,而不是仅向我显示每半年或每月支付的新客户。

我该如何解决这个问题?

我试过括号等等,但都无济于事;我是否只需要为每种情况创建一个if并手动编写每个filterexpression?

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    SqlDataSource1.FilterExpression = string.Empty;
    if (MonthlyCheckBox.Checked)
    {

            SqlDataSource1.FilterExpression = "[CustomerSubscriptionType]='Monthly'";
    }
    if (SemiAnnuallyCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += "OR ([CustomerSubscriptionType]='Semi-Annually')";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerSubscriptionType]='Semi-Annually'";
        }

    }
    if (AnnuallyCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += "OR ([CustomerSubscriptionType]='Annually')";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerSubscriptionType]='Annually'";
        }
    }
    if (NewCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += " ( OR [CustomerStatus]='Active')";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerStatus]='New'";
        }

    }
    if (ActiveCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += " OR [CustomerStatus]='Active'";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerStatus]='Active'";
        }
    }
    if (SuspendedCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += " OR [CustomerStatus]='Suspended'";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerStatus]='Suspended'";
        }
    }
    if (ResidentialCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
                SqlDataSource1.FilterExpression += " AND [CustomerType]='Residential' ";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerType]='Residential'";
        } 
    }
    if (CommercialCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += " AND [CustomerType]='Commercial' ";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerType]='Commercial'";
        } 

    }
    if (NotInGroupCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += " AND [CustomerGroup]=''";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerGroup]=''";
        }
    }
    if (InGroupCheckBox.Checked)
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += " AND [CustomerGroup]<>''";
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerGroup]<>''";
        }
    }
    if (GroupDropDownFilter.Text != "Select...")
    {
        if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
        {
            SqlDataSource1.FilterExpression += " AND [CustomerGroup]='{0}'";
            SqlDataSource1.FilterParameters.Add("@CustomerGroup", GroupDropDownFilter.Text);
        }
        else
        {
            SqlDataSource1.FilterExpression = "[CustomerGroup]='{0}'";
            SqlDataSource1.FilterParameters.Add("@CustomerGroup", GroupDropDownFilter.Text);
        }
    }
}

1 个答案:

答案 0 :(得分:4)

您正在使用OR运算符。

if(@CustomerSubscriptionType = 'Monthly' OR @CustomerStatus = 'New')将为您提供每月支付的所有客户(有效和新用户)以及所有新客户(无论订阅类型如何)。

如果您使用AND运营商,则只能让新客户每月付费。

您可能希望使用两个运算符的组合,因此我会在组中使用OR运算符,并在组之间使用AND运算符。

if(
    (@CustomerSubscriptionType = 'Monthly' OR @CustomerSubscriptionType = 'Semi-Monthly')
AND (@CustomerStatus = 'New')
AND (@SomeOtherCriteria = 'Other' OR @SomeOtherCriteria = 'Other2')
AND etc...

要修复代码,我会为每个“组”使用单独的字符串:

SqlDataSource1.FilterExpression = string.Empty;

List<string> subscriptionTypeFilter = new List<string>();
if (MonthlyCheckBox.Checked)
{
    subscriptionTypeFilter.Add("[CustomerSubscriptionType]='Monthly'");
}

if (SemiAnnuallyCheckBox.Checked)
{
    subscriptionTypeFilter.Add("[CustomerSubscriptionType]='Semi-Annually'");
}

if (AnnuallyCheckBox.Checked)
{
subscriptionTypeFilter.Add("[CustomerSubscriptionType]='Annually'");
}

List<string> customerStatusFilter = new List<string>();
if (NewCheckBox.Checked)
{
subscriptionTypeFilter.Add("[CustomerStatus]='New'");             
}

if (ActiveCheckBox.Checked)
{
subscriptionTypeFilter.Add("[CustomerStatus]='Active'");
}

if (SuspendedCheckBox.Checked)
{
subscriptionTypeFilter.Add("[CustomerStatus]='Suspended'");
}


List<string> filters = new List<string>();

filters.Add("(" + string.Join(" OR ", subscriptionTypeFilter) + ")");
filters.Add("(" + string.Join(" OR ", customerStatusFilter) + ")");

SqlDataSource1.FilterExpression = string.Join(" AND ", filters);      
  

编辑:是的,我的意思是“为每种情况创建一个if并手动编写每个filterexpression”。我希望我不必这样做,并可以让它以其他方式工作

要动态执行您想要的操作,您可以在元素上指定字段名称和值作为自定义属性:

<asp:CheckBox id="SuspendedCheckBox" runat="server" fieldName="CustomerStatus" fieldValue="Suspended" />

一旦正确定义了所有复选框,只需循环控制。

foreach(Control c in this.Controls)
{
    if(c is CheckBox)
    {
        CheckBox cb = (CheckBox)c;

        if(cb.IsChecked)
        {
            string fieldName = cb.Attributes["fieldName"];
            string fieldValue = cb.Attributes["fieldValue"];

            someDictionaryMaybe.Add(string.Format("[{0}] = '{1}'", fieldName, fieldValue), fieldName);
        }
    }
}

// loop through the dictionary, joining the different expressions with ORs and ANDs
// or use LINQ to put it all together, it's up to you.  The fieldName was added to 
// the dictionary, which can be used to separate with ANDs and ORs.