简体   繁体   English

将过滤器表达式动态添加到Gridview中以过滤两列

[英]Dynamically add Filter Expression to the Gridview to Filter Two Columns

Please see the following code: 请参见以下代码:

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataSource1.FilterExpression = "";
        foreach (ListItem l in CheckBoxList1.Items)
        {
            if (l.Selected)
            {
                HD1.Value += l.Value + ",";
                if (SqlDataSource1.FilterExpression == "")
                    SqlDataSource1.FilterExpression += " TSOType like '" + l.Value + "%'";
                else
                {
                    SqlDataSource1.FilterExpression += "or TSOType like '" + l.Value + "%'";
                }

            }
        }
    }

I am dynamically assign filter expression to the Gridview when the items in checkbox is checked. 当复选框中的项目被选中时,我将过滤器表达式动态分配给Gridview。 This will give me the expected result when Cloumn "TSOType" meet the requirements. 当Cloumn“ TSOType”满足要求时,这将给我预期的结果。 What I want now is to add one more column filter to the FilterExpression. 我现在想要的是在FilterExpression中再添加一个列过滤器。 For example, now I have 例如,现在我有

SqlDataSource1.FilterExpression += " TSOType like '" + l.Value + "%'";

But I want make it to 但我想做到

SqlDataSource1.FilterExpression += " TSOType like '" + l.Value + "%' + TSOStatus like '" + j.Value + "%'";

How should I code this, do I need another for loop? 我应该如何编码,是否需要另一个for循环?

The SqlDataSource control supports filtering data only when in the DataSet mode. SqlDataSource控件仅在处于DataSet模式时才支持过滤数据。

Refer here for details. 详情请参阅此处 So add DataSourceMode="DataSet" for the SqlDataSource object. 因此,为SqlDataSource对象添加DataSourceMode="DataSet"

And you can use AND operator to add multiple conditions for the FilterExpression 您可以使用AND运算符为FilterExpression添加多个条件

SqlDataSource1.FilterExpression += " TSOType like '" + l.Value + "%' + AND TSOStatus like '" + j.Value + "%'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM