How to filter DataView based on multiple inputs

前端 未结 3 1103
执念已碎
执念已碎 2020-12-12 04:51

I know how to filter data based on the user\'s input from a single textbox:

FilterDataView.RowFilter = txtFilter.Text;

But how would you go about filtering d

3条回答
  •  我在风中等你
    2020-12-12 05:04

    You can use something like light t-sql when defining RowFilter.

    One idea is:

    FilterDataView.RowFilter = "name like '%habjan%' and city like '%new york%'"
    

    Here you can find a good article about RowFilter syntax: DataView RowFilter Syntax

    For what you need you will have to build row filter based on entered fields.

        StringBuilder sb = new StringBuilder();
    
        if (tb1.Text.Length > 0)
        {
           sb.Append("name like '%" + tb1.Text + "%'");
        }
    
        if (tb2.Text.Length > 0)
        {
           if(sb.Length > 0)
           {
               sb.Append(" and ");
           }
    
           sb.Append("city like '%" + tb2.Text + "%'");
        }
        //.... and so on...
    
        FilterDataView.RowFilter = sb.ToString();
    

提交回复
热议问题