【问题标题】:How can we apply multiple filters on DataGridView by passing the filtered data to the second filter and so on?我们如何通过将过滤后的数据传递给第二个过滤器等来在 DataGridView 上应用多个过滤器?
【发布时间】:2021-04-02 11:27:30
【问题描述】:

我的 DataGridView 中有列(“Region”、“Customer”、“Salesman”、“Brand”、“Groups”、“Category”和“Product”)现在我想首先应用基于“Region”的过滤器然后在过滤后的数据上想要对“客户”应用过滤器,并希望对过滤后的数据应用“销售员”过滤器。我用过这段代码

        private void BtnApplyFilter_Click(object sender, EventArgs e)
        {
            (SalesView.DataSource as DataTable).DefaultView.RowFilter = null;
            if (RegionName != "All")
            {
                (SalesView.DataSource as DataTable).DefaultView.RowFilter = string.Format("Region='{0}'", CbRegions.Text);
            }
            if (CustomerName != "All")
            {
                (SalesView.DataSource as DataTable).DefaultView.RowFilter = string.Format("Customer='{0}'", CbCustomers.Text);
            }
            if (SalesmanName != "All")
            {

                (SalesView.DataSource as DataTable).DefaultView.RowFilter = string.Format("Salesman='{0}'", CbSalesmen.Text);
            }
            if (BrandName != "All")
            {
                (SalesView.DataSource as DataTable).DefaultView.RowFilter = string.Format("Brand='{0}'", CbBrands.Text);
            }
            if (GroupName != "All")
            {
                (SalesView.DataSource as DataTable).DefaultView.RowFilter = string.Format("Groups='{0}'", CbGroups.Text);
            }
            if (CategoryName != "All")
            {
                (SalesView.DataSource as DataTable).DefaultView.RowFilter = string.Format("Category='{0}'", CbCategories.Text);
            }
            if (ItemName != "All")
            {
                (SalesView.DataSource as DataTable).DefaultView.RowFilter = string.Format("ProductName='{0}'", CbProducts.Text);
            }
            SalesView.ClearSelection();
            GetTotals();

        }

我尝试使用“AND”运算符将所有过滤器组合在一个字符串中,但这不起作用,因为“All”值就像用户选择“All”时一样,在过滤器字符串中我用空字符串替换“All”不工作我也试过 Like 运算符。现在任何人都可以告诉我如何使用/将过滤后的数据提供给第二个过滤器,然后将过滤后的数据提供给第三个过滤器,依此类推?因为当我应用第二个过滤器时,它不会应用于过滤后的数据,而是应用于原始完整数据。

【问题讨论】:

标签: c# filter datagridview datagrid


【解决方案1】:

“AND”运算符应该可以工作,但您没有向我们展示该代码,所以我无法告诉您为什么它在您的情况下不起作用。因此,尝试类似

    var criteria = new List<string>();

    if (RegionName != "All")
    {
       criteria.Add(string.Format("Region='{0}'", CbRegions.Text));
    }
    if (CustomerName != "All")
    {
        criteria.Add(string.Format("Customer='{0}'", CbCustomers.Text);
    }
    // ...
    string condition = string.Join(" AND ",criteria);
    (SalesView.DataSource as DataTable).DefaultView.RowFilter = condition;
  

还有一个额外的提示:如果你看到代码在一行中重复了好几次

(SalesView.DataSource as DataTable).DefaultView.RowFilter = /*...*/

帮自己一个忙,将其重构为只执行一次分配的变体,否则您将产生维护噩梦。

【讨论】:

    【解决方案2】:

    你应该使用“或”和“真”。

    例如: (Customer='{0}' OR true) AND (Region='{0}' OR true) ...

    【讨论】:

    • 对我来说看起来过于复杂了。任何解释为什么这应该是必要的?
    • 其实这个“if”语句是不必要的。过滤可以在一行中处理:(Customer='{0}' OR true) AND (Region='{0}' OR true)... => 这意味着,如果“Customer={0}”中不存在数据过滤器,“OR true”语句已添加到此过滤器,第二个过滤器也将运行。
    猜你喜欢
    • 2020-05-04
    • 2021-01-29
    • 1970-01-01
    • 2022-12-06
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 2022-08-02
    • 2015-07-03
    相关资源
    最近更新 更多