Reputation: 599
Can I make multiple filter in gridview? So far I have three separate methods for each kind of filtering. I want to be able to do something like this. First choose from combobox value which will be shown and then from this filtered list, I would like to be able to search by using textbox for something else.
private void button9_Click(object sender, EventArgs e)
{
var result = list3.Where(Srodek => Srodek.Srodek.ID.Device == textBox2.Text).ToList();
dataGridView4.DataSource = result;
}
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
var result = list3.Where(Srodek => Srodek.Srodek.category1 == comboBox1.SelectedItem.ToString()).ToList();
dataGridView4.DataSource = result;
}
Now when I choose some value from combobox it show what I want in gridview, but later when I insert something in textbox and click the button, it is filtering the whole list not this one already filtered by combobox. How can I achieve it?
Upvotes: 1
Views: 956
Reputation: 3834
Try FilterDataGrid()
calling on both events:
private void button9_Click(object sender, EventArgs e)
{
FilterDataGrid();
}
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
FilterDataGrid();
}
private void FilterDataGrid()
{
var _text = Convert.ToString(textBox2.Text);
var _comboText = ! string.IsNullOrEmpty(comboBox1.Text) ? Convert.ToString(comboBox1.SelectedItem) : string.Empty;
var result = list3.Where(Srodek => Srodek.Srodek.category1 == _comboText || Srodek.Srodek.ID.Device == _text).ToList();
//
dataGridView4.DataSource = result;
}
Hopes this helps u.
Upvotes: 3
Reputation: 63317
I suppose the type of element in list3
is T
:
private void button9_Click(object sender, EventArgs e)
{
if(dataGridView4.DataSource is IEnumerable<T>){
var result = ((IEnumerable<T>)dataGridView4.DataSource).Where(Srodek => Srodek.Srodek.ID.Device == textBox2.Text).ToList();
dataGridView4.DataSource = result;
}
}
Upvotes: 1
Reputation: 703
You aren't changing the source of you filter, so you are filtering on the same set each time. One option would be to have the original dataset and a filtered dataset, then when you filter the original save the results in the filtered set.
Upvotes: 0
Reputation: 158
Make a global property that will host your list so that everytime you perform a query on the list you perform a query on your up to date list that has already been filtered.
Upvotes: 0