用两个文本框过滤DataGridView [英] Filtering DataGridView with two textboxes

查看:93
本文介绍了用两个文本框过滤DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DGV,我已经创建了一个数据源和表适配器。在表中,我已经定义了两个参数进行搜索。所以我有一个WPF与数据网格视图和两个文本框,我将用来进行搜索。

  public partial FormConsultaAdvogadosDS:Form 
{
public FormConsultaAdvogadosDS()
{
InitializeComponent();
}

private void FormConsultaAdvogadosDS_Load(object sender,EventArgs e)
{
// TODO:这行代码将数据加载到dataSetAdvogados.Advogado表中。您可以根据需要移动或移除它。
this.advogadoTableAdapter.Fill(this.dataSetAdvogados.Advogado);

}

private void textBox1_TextChanged(object sender,EventArgs e)
{
try
{
this.advogadoTableAdapter。 FillBy(this.dataSetAdvogados.Advogado,textBox1.Text);
}
catch(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}

private void textBox2_TextChanged(object sender,EventArgs e)
{
try
{
this.advogadoTableAdapter .FillBy1(this.dataSetAdvogados.Advogado,textBox2.Text);
}
catch(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}

这是我有马上。
我只用一个文本框进行过滤就可以了,它过滤掉所有的OK。但是我真正需要的是第一次过滤器中的过滤器。像第一个filteing的依赖。
如果我在文本框1中插入文本ABC,显示的行具有ABC,然后使用第一个过滤器,我需要在textbox2DEF中进行细化插入,但不起作用。它使一个新的搜索忽略第一个过滤。有人能帮我吗?
注意

解决方案

当您过滤一个集合时,通常需要管理两个集合。一个是原始的,未过滤的集合,其中包含可用值/项目的所有,另一个是仅包含与过滤条件匹配的项目的过滤集合。



在您有两个过滤器的情况下,您需要管理三个集合。一个是原始的,未过滤的集合,第二个是仅包含与第一个过滤条件匹配的项目的第一个过滤集合,第三个是仅包含与第二个条件匹配的项目的第二个过滤集合。



所以如果这不清楚,这里是一个例子。我们有一种情况,用户输入 ABC ,第二个集合返回与该搜索匹配的原始(完整)集合中的所有项目 string 。现在这里是重要的一部分。而不是再次使用第二个过滤器过滤原始集合,我们需要过滤已经由 ABC字符串过滤的第二个集合。 / p>

第三个集合是绑定到UI的数据集,只显示原始集合中符合 过滤条件的项目。






更新>>>



作为另一个例子,您可以使用 LinQ 过滤您的第二个集合(已被第一个条件过滤):

  ThirdCollection = new ObservableCollection< string>(SecondCollection.Where(i => 
SecondFilterConditionMethod(i)));

现在这个 SecondFilterConditionMethod 方法可以包含任何类型的条件只要它返回 true false




$ b {
//在这里实现任何过滤条件
return item.StartsWith(A);

  private bool SecondFilterConditionMethod 
}

现在您可能不会使用 string s,但是这个想法是一样的,不管你使用的数据类型如何。另请参阅我对根据文本框文本更改组合框项目列表在Stack Overflow上进一步的示例。


I have a DGV that i've created with a datasource and a table adapter. In the table adapdter i've defined two parameters to make a search. So then i have a WPF with the data grid view and TWO textboxes wich i'll use to make the search.

 public partial class FormConsultaAdvogadosDS : Form
{
  public FormConsultaAdvogadosDS()
    {
        InitializeComponent();
    }

    private void FormConsultaAdvogadosDS_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'dataSetAdvogados.Advogado' table. You can move, or remove it, as needed.
        this.advogadoTableAdapter.Fill(this.dataSetAdvogados.Advogado);

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        try
        {
            this.advogadoTableAdapter.FillBy(this.dataSetAdvogados.Advogado, textBox1.Text);
        }
        catch (System.Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        try
        {
            this.advogadoTableAdapter.FillBy1(this.dataSetAdvogados.Advogado, textBox2.Text);
        }
        catch (System.Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
    }
}

This is the code i have right now. Whe i do the filtering with just one of the textboxes it's all ok, it filters all ok. But what i really need is a filter inside the first filtering. Like dependence of the first filteing. If i insert in the textbox1 the text "ABC" the rows displayed have "ABC" and then, with this first filter, i need to refine inserting in textbox2 "DEF", but it doesn't work. It make a new search ignoring the first filtering. Can someone help me? Regards

解决方案

When you filter one collection, you typically need to manage two collections. One is the original, unfiltered collection that contains all of the available values/items and the other is the filtered collection that contains only the items that match the filter condition.

In your case of having two filters, you'll need to manage three collections. One is the original, unfiltered collection, the second is the first filtered collection that contains only the items that match the first filter condition and the third is the second filtered collection that contains only the items that match the second condition.

So if that wasn't clear, here's an example. We have a situation where the user enters ABC and the second collection returns all items from the original (full) collection that matches that search string. Now here is the important part. Instead of filtering the original collection again with the second filter, we need to filter the second collection that has already been filtered by the ABC string.

The third collection is the one that is data bound to the UI and will only show the items from the original collection that meet both filter conditions.


UPDATE >>>

As a further example, you can use LinQ to filter your second collection (that has already been filtered by the first condition):

ThirdCollection = new ObservableCollection<string>(SecondCollection.Where(i => 
    SecondFilterConditionMethod(i)));

Now this SecondFilterConditionMethod method can contain any type of condtion as long as it returns either true or false:

private bool SecondFilterConditionMethod(string item)
{
    // implement whatever filter condition here
    return item.StartsWith("A");
}

Now you might not be using strings, but the idea is just the same, regardless of the data type that you are using. Please also take a look at my answer to the Change combo box item list depending on the Textbox Text question here on Stack Overflow for a further example.

这篇关于用两个文本框过滤DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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