Reputation: 249
I have a list of filters that the user has input and I want to apply them to my DataGridView
. What is the best way to apply all the filters to a DataGridView
? My list of ColumnFilters
is basically a list of strings which are broken into : Column name, operand (=, >, < etc.)
and the value of the user input.
public void ApplyFilters(List<ColumnFilter> filters)
{
if (filters.Count > 0))
{
foreach (ColumnFilter filter in filters)
{
BindingSource bs = (BindingSource)dataGridView1.DataSource;
bs.Filter = string.Format("{0} {1} '{2}'", filter.ColumnName, filter.Operand, filter.Value);
dataGridView1.DataSource = bs;
}
}
}
Upvotes: 0
Views: 96
Reputation: 12544
The bindingsource Filter
supports AND
, so the filters could be combined with something like:
bs.Filter = string.Join(" AND ", filters.Select(filter=>string.Format("{0} {1} '{2}'", filter.ColumnName, filter.Operand, filter.Value).ToArray());
(Note: the foreach is not needed when using the above)
Upvotes: 2