AC3
AC3

Reputation: 11

c# Datagridview multiple filters

I am trying to filter a datagrid with two or more textboxes. Is there a simple way to enable this? As it is now the datagrid is only filtered on the last criteria checked.

I have searched all day and could not find a solution.

This is the code for the two boxes. I added the two filters to the last box but this was just a guess and I knew it probably wouldn't work.

Thanks, A.

private void textBox4_TextChanged(object sender, EventArgs e)
    {
        BindingSource bs = new BindingSource();
        bs.DataSource = dataGridView1.DataSource;
        bs.Filter = "Bank like '%" + textBox4.Text + "%'";
        dataGridView1.DataSource = bs;
    }

    private void textBox5_TextChanged(object sender, EventArgs e)
    {
        BindingSource bs = new BindingSource();
        bs.DataSource = dataGridView1.DataSource;

        bs.Filter = "Bank like '%" + textBox4.Text + "%'";
        bs.Filter = "currency like '" + textBox5.Text + "'";

        dataGridView1.DataSource = bs;
    }

Upvotes: 0

Views: 6370

Answers (3)

Anders Hansson
Anders Hansson

Reputation: 1

Simple wpf application using MVVM, three Textbox as filter and one datagrid.

MainWindow.xml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0" Orientation="Horizontal">
        <TextBox Text="{Binding Filter1, UpdateSourceTrigger=PropertyChanged}" Width="100"/>
        <TextBox Text="{Binding Filter2, UpdateSourceTrigger=PropertyChanged}" Width="100"/>
        <TextBox Text="{Binding Filter3, UpdateSourceTrigger=PropertyChanged}" Width="100"/>
    </StackPanel>
    <DataGrid Grid.Row="1" ItemsSource ="{Binding ItemView}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Head 1" Binding="{Binding Text1}"/>
            <DataGridTextColumn Header="Head 2" Binding="{Binding Text2}"/>
            <DataGridTextColumn Header="Head 3" Binding="{Binding Text3}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

ViewModel.cs

    public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    List<Items> _items;
    Items _item;
    private static Random random = new Random();
    const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private string _filter1 = "";
    private string _filter2 = "";
    private string _filter3 = "";

    public ViewModel()
    {
        _items = new List<Items>();

        for (int i = 0; i < 1000; i++)
        {
            _item = new Items();
            _item.Text1 = new string(Enumerable.Repeat(chars, 10).Select(s => s[random.Next(s.Length)]).ToArray());
            _item.Text2 = new string(Enumerable.Repeat(chars, 10).Select(s => s[random.Next(s.Length)]).ToArray());
            _item.Text3 = new string(Enumerable.Repeat(chars, 10).Select(s => s[random.Next(s.Length)]).ToArray());
            _items.Add(_item);
        }
        ItemList = new ObservableCollection<Items>(_items);
        ItemView = (CollectionView)CollectionViewSource.GetDefaultView(ItemList);
        ItemView.Filter = TextFilter;

    }
    private bool TextFilter(object obj)
    {
        var data = obj as Items;
        if (data != null)
        {
            return data.Text1.StartsWith(_filter1) && data.Text2.StartsWith(_filter2) && data.Text3.StartsWith(_filter3);
        }
        return false;
    }

    private void NotifyPropertyChanged(string property)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
    }

    public ObservableCollection<Items> ItemList { get; set; }
    public CollectionView ItemView { get; set; }
    public string Filter1
    {
        get { return _filter1; }
        set
        {
            _filter1 = value;
            NotifyPropertyChanged("Filter1");
            ItemView.Refresh();
        }
    }
    public string Filter2
    {
        get { return _filter2; }
        set
        {
            _filter2 = value;
            NotifyPropertyChanged("Filter2");
            ItemView.Refresh();

        }
    }
    public string Filter3
    {
        get { return _filter3; }
        set
        {
            _filter3 = value;
            NotifyPropertyChanged("Filter3");
            ItemView.Refresh();
        }
    }

}

public class Items
{
    public string Text1 { get; set; }
    public string Text2 { get; set; }
    public string Text3 { get; set; }

}

Upvotes: 0

oh_no_oh_no
oh_no_oh_no

Reputation: 1

BindingSource.DataSource Create new DataView({DataTable})

BindingSource source1 = new BindingSource();
source1.DataSource = new DataView(ThreadModule.CaseManagerThread.caseListDataTable);
source1.Filter = "status = 0";
this.NewCaseTabControl.newCaseDataGridView.DataSource = source1;

BindingSource source2 = new BindingSource();
source2.DataSource = new DataView(ThreadModule.CaseManagerThread.caseListDataTable);
source2.Filter = "status = 1";
this.FinishCaseTabControl.finishCaseDataGridView.DataSource = source2;

Upvotes: 0

Fernando
Fernando

Reputation: 21

try something like this.

qualityBindingSource.Filter = string.Format("Date_ >= '{0}' AND Date_ <= '{1}'", dateTimePicker1.Text, dateTimePicker2.Text);

Upvotes: 2

Related Questions