Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

c# - How to filter DataView based on multiple inputs

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 data based on multiple user input from multiple fields. Basically filter would act as a "search" functionality.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...