简体   繁体   English

C#将多个过滤器应用于datagridview

[英]C# apply more than one filter to datagridview

I have a list of filters that the user has input and I want to apply them to my DataGridView . 我有用户输入的过滤器列表,我想将其应用到DataGridView What is the best way to apply all the filters to a DataGridView ? 将所有过滤器应用于DataGridView的最佳方法是什么? My list of ColumnFilters is basically a list of strings which are broken into : Column name, operand (=, >, < etc.) and the value of the user input. 我的ColumnFilters列表基本上是一个字符串列表,这些字符串分为: Column name, operand (=, >, < etc.)和用户输入的值。

public void ApplyFilters(List<ColumnFilter> filters)
{
    if (filters.Count > 0))
    {
        foreach (ColumnFilter filter in filters)
        {
            BindingSource bs = (BindingSource)dataGridView1.DataSource; 
            bs.Filter = string.Format("{0} {1} '{2}'", filter.ColumnName, filter.Operand, filter.Value);
            dataGridView1.DataSource = bs;
        }
     }
}

The bindingsource Filter supports AND , so the filters could be combined with something like: bindingsource Filter支持AND ,因此可以将过滤器与以下内容结合使用:

bs.Filter  = string.Join(" AND ", filters.Select(filter=>string.Format("{0} {1} '{2}'", filter.ColumnName, filter.Operand, filter.Value).ToArray());

(Note: the foreach is not needed when using the above) (注意:使用上述方法时不需要foreach)

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

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