0

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

1 Answer 1

0

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.

1
  • OK, i've understood your explanation. But, i don't really know how i can manage the collections! The method that i've used consists in the creation of a dataset and then I define parameters in the table adapter. I'm seeing my Fill, FillBy and FillBy1 as collections. Filtering the first it's OK as you said. But i don't know how to get the last filtering (FILLBY "ABC") to make another filtering above it (FILLBY1 (FILLBY) "DEF"). It's my really problem.
    – Reznor13
    Mar 20, 2014 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.