简体   繁体   中英

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. This will give me the expected result when Cloumn "TSOType" meet the requirements. What I want now is to add one more column filter to the 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?

The SqlDataSource control supports filtering data only when in the DataSet mode.

Refer here for details. So add DataSourceMode="DataSet" for the SqlDataSource object.

And you can use AND operator to add multiple conditions for the FilterExpression

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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