如何基于多个输入过滤DataView [英] How to filter DataView based on multiple inputs

查看:145
本文介绍了如何基于多个输入过滤DataView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



FilterDataView.RowFilter = txtFilter.Text;


我知道如何根据用户从单个文本框的输入过滤数据。

但是,您将如何去基于多个字段的多个用户输入来过滤数据。
基本上过滤器将起到搜索功能的作用。

解决方案

定义RowFilter。



一个想法是:
$ b $ pre $ FilterDataView.RowFilter = 像'%habjan%'这样的名字和像'%new york%'这样的城市'

找到一个关于RowFilter语法的好文章: DataView RowFilter语法





 

code> 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 +%');
}
// ....等等...

FilterDataView.RowFilter = sb.ToString();


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.

解决方案

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

这篇关于如何基于多个输入过滤DataView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆