所以,我有一个GridView
“CustomerGridView”和一个SqlDataSource
“SqlDataSource1”,它们从我的 sql 表中提取数据。现在我想添加一些过滤,所以我创建了一个带有各种选项的侧面板。
我的问题是,使用我当前的代码,如果我选择,说客户每月支付一次,新客户,还有每半年支付一次的客户。它向我展示了所有这些类型,包括符合标准的活跃(非新)客户,而不是只向我展示半年或每月付款的新客户。
我怎样才能解决这个问题?
我试过括号等,但都无济于事;我是否只需要为每种情况创建一个 if 并手动编写每个过滤器表达式?
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);
}
}
}