【问题标题】:Multiple filters on DataGridView and null valuesDataGridView 和空值上的多个过滤器
【发布时间】:2016-01-18 08:52:04
【问题描述】:

所以我有一个包含许多列的 datagridview,其中两个是“状态”和“分配给”。我有两个字段,用户应该能够根据输入的内容过滤所有行。我的代码如下所示:

status = textBox1.Text;
allocated = textBox2.Text;

BindingSource bs = new BindingSource();
bs.DataSource = customerDataGridView.DataSource;

bs.Filter = "[Status] LIKE '%" + status + "%' AND [AssignedTo] LIKE '%" + allocated + "%'";
customerDataGridView.DataSource = bs;

如果用户同时输入了状态和分配,这将很有效,因为它会找到具有这两个过滤器的所有记录。但是,如果其中一个过滤器留空,例如 Status = "Pending" 和 Allocation = "",它将按预期过滤记录,因此它将过滤 Status 为 Pending 且 Allocated 为空的所有字段。问题是,如果用户将 Allocated 留空,我希望它只忽略过滤器中的这个字段,并简单地过滤所有 Pending 记录而不考虑分配。

有什么想法可以实现这一点吗?

编辑:我应该提一下,为了简单起见,我在这里只包含了两个过滤器。我的程序实际上有 6 个过滤器,所以过滤器字符串需要 6 个参数。

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    那么 if 子句怎么样?

        string filterString = ""
    
        if(!string.IsNullOrEmpty(status)){
                filterString += "[Status] LIKE '%" + status + "%'";
        }
        if (!string.IsNullOrEmpty(allocated){
            if(!string.isNullOrEmpty(filterString) filterString += " AND ";
            filterString += "[AssignedTo] LIKE '%" + allocated + "%'";
        }
    

    【讨论】:

    • 是的,我考虑过,但实际上用户可以填写大约 6 个字段来过滤,而不仅仅是我在这里提到的两个。为了简单起见,我只提到了两个。因此,如果有 6 个字段,我将必须有一个包含 36 个条件的 if 子句来涵盖所有可能性。这会起作用,但我希望有一种更简单的方法来实现相同的结果。
    • @nerdalert 确实如此。如果您为每个字段创建一个 if 子句并将过滤器的一部分添加到“filterString”,最后将过滤器设置为该字符串?
    • 这似乎可以很好地工作!我会试一试并报告。
    • 我接受了 Hendra Lim 的回答,因为它有一个有效的解决方案,因此查看此问题的其他人将看到有效的代码。我希望我能接受两个答案,因为您首先有这个想法,但无论如何谢谢!
    • 我同意。我懒得做整件事:D
    【解决方案2】:
        status = textBox1.Text;
        allocated = textBox2.Text;
    
        BindingSource bs = new BindingSource();
        bs.DataSource = customerDataGridView.DataSource;
    
       var filt = "";
    
    
        if (!string.IsNullOrEmpty(status))
           {
             if(filt == "")
                filt  += "[Status] LIKE '%" + status + "%'";
             else
                filt += " And [Status] LIKE '%" + status + "%' ";
           }
    
    
          if (!string.IsNullOrEmpty(allocated))
               {
                 if(filt == "")
                    filt  += "[AssignedTo] LIKE '%" + allocated + "%'";
                 else
                    filt += " And [AssignedTo] LIKE '%" + allocated + "%' ";
               }
    
             if (!string.IsNullOrEmpty(param3))
              {
                 if(filt == "")
                    filt  += "[param3] LIKE '%" + param3+ "%'";
                 else
                    filt += " And [param3] LIKE '%" + param3+ "%' ";
              }
    
         bs.Filter = filt ;
        customerDataGridView.DataSource = bs;
    

    等等..

    这个例子只有3个参数,你可以添加更多参数,你需要做的只是“在将它设置为bs.filter之前在filt变量中添加更多过滤器”。 希望你能明白。

    【讨论】:

    • 是的,这与 S. Jerk 在评论中建议的方法相同。这似乎是最好的方法。感谢您的工作示例!
    【解决方案3】:

    试试下面的代码

    status = textBox1.Text;
    allocated = textBox2.Text;
    
    BindingSource bs = new BindingSource();
    bs.DataSource = customerDataGridView.DataSource;
    
    if (!string.IsNullOrEmpty(status) && !string.IsNullOrEmpty(allocated))
        bs.Filter = "[Status] LIKE '%" + status + "%' AND [AssignedTo] LIKE '%" + allocated + "%'"; // both filter assign.
    else if (string.IsNullOrEmpty(status))
        bs.Filter = "[AssignedTo] LIKE '%" + allocated + "%'"; // only allocated filter assign.
    else //(string.IsNullOrEmpty(allocated))
        bs.Filter = "[Status] LIKE '%" + status + "%'"; // only status filter assign.
    
    customerDataGridView.DataSource = bs;
    

    【讨论】:

    • 我通过编辑更新了我的帖子。所以虽然这会很好,但你知道我需要 36 个子句。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多