简体   繁体   中英

Filter WPF DataGrid by multiple criterias

I would like to implement Excel like filter to datagrid by three columns.

Here is my code:

    using System.Data.Odbc;
    using System.Windows;
    using System.Data;

    namespace DB_inspector
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();

            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                OdbcConnection dbConnection = new OdbcConnection("Driver={Pervasive ODBC Client Interface};ServerName=875;dbq=@DBFS;Uid=Username;Pwd=Password;");
                string strSql = "select NRO,NAME,NAMEA,NAMEB,ADDRESS,POSTA,POSTN,POSTADR,COMPANYN,COUNTRY,ID,ACTIVE from COMPANY";
                dbConnection.Open();
                OdbcDataAdapter dadapter = new OdbcDataAdapter();
                dadapter.SelectCommand = new OdbcCommand(strSql, dbConnection);
                DataTable table = new DataTable("COMPANY");
                dadapter.Fill(table);
                DataGrid1.DataContext = table;
                DataGrid1.ItemsSource = table.DefaultView;
                dadapter.Update(table);
                dbConnection.Close();
            }

        private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            DataView dv = DataGrid1.ItemsSource as DataView;

            string filter = NameSearch.Text;
                if (string.IsNullOrEmpty(filter))
                    dv.RowFilter = null;
                else
                    dv.RowFilter = string.Format("NAME Like '%{0}%'", filter);

        }

        private void TextBox_TextChanged_1(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            DataView dv = DataGrid1.ItemsSource as DataView;

            string filter = ActiveSearch.Text;
            if (string.IsNullOrEmpty(filter))
                dv.RowFilter = null;
            else
                dv.RowFilter = string.Format("ACTIVE Like '%{0}%'", filter);
        }

        private void CustomerNumberSearch_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            DataView dv = DataGrid1.ItemsSource as DataView;

            string filter = CustomerNumberSearch.Text;
            if (string.IsNullOrEmpty(filter))
                dv.RowFilter = null;
            else
                dv.RowFilter = string.Format("NRO Like '%{0}%'", filter); //this should be "Begins with" not "Like"
        }
    }
}

XAML

<Window x:Class="DB_inspector.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DB_inspector"
        mc:Ignorable="d"
        Title="DB database inspector" Height="595.404" Width="1005.571">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="11*"/>
            <RowDefinition Height="553*"/>
        </Grid.RowDefinitions>
        <DataGrid x:Name="DataGrid1" Margin="0,51,0,0" Grid.Row="1"/>
        <Image Height="41" Margin="847,10,10,502" Width="141" Source="Logo_small.jpg" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Top"/>
        <TextBox x:Name="NameSearch" HorizontalAlignment="Left" Height="23" Margin="77,14,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="409" Grid.Row="1" TextChanged="TextBox_TextChanged"/>
        <Button Content="Refrsh" HorizontalAlignment="Left" Height="23" Margin="736,14,0,0" VerticalAlignment="Top" Width="72" Click="Button_Click" Grid.Row="1" Background="#FF51C951" BorderBrush="{x:Null}" Foreground="White"/>
        <TextBox x:Name="ActiveSearch" HorizontalAlignment="Left" Height="23" Margin="502,14,0,0" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="66" TextChanged="TextBox_TextChanged_1"/>
        <TextBox x:Name="CustomerNumberSearch" HorizontalAlignment="Left" Height="23" Margin="580,14,0,0" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="82" TextChanged="CustomerNumberSearch_TextChanged"/>
        <ProgressBar x:Name="ProgressBar" HorizontalAlignment="Left" Height="10" Margin="0,1,0,0" VerticalAlignment="Top" Width="998" BorderBrush="{x:Null}" Background="{x:Null}"/>
        <Button Content="Button" HorizontalAlignment="Left" Height="23" Margin="680,14,0,0" Grid.Row="1" VerticalAlignment="Top" Width="51" Click="Button_Click_1"/>

    </Grid>
</Window>

I would like to implement filters for column NRO and ACTIVE so that during load of data in column NRO will be numbers starting with 3 only and in column ACTIVE values with 1 only. In ACTIVE there are values 1 and 0 (1 active, 0 not active).

My current setup is filtering only by one criteria. How to make all three criteria function correct?

you need to combine multiple conditions in one Filter string:

private Dictionary<string, string> _conditions = new Dictionary<string, string>();

private void UpdateFilter()
{
     var activeConditions = _conditions.Where(c => c.Value != null).Select(c => "(" + c.Value + ")");
     DataView dv = DataGrid1.ItemsSource as DataView;
     dv.RowFilter = string.Join(" AND ", activeConditions);
}

private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
    string filter = NameSearch.Text;
    if (string.IsNullOrEmpty(filter))
        _conditions["name"] = null;
    else
        _conditions["name"] = string.Format("NAME Like '%{0}%'", filter);
    UpdateFilter();
}

private void TextBox_TextChanged_1(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
    string filter = ActiveSearch.Text;
    if (string.IsNullOrEmpty(filter))
        _conditions["active"] = null;
    else
        _conditions["active"] = string.Format("ACTIVE Like '%{0}%'", filter);
    UpdateFilter();
}

private void CustomerNumberSearch_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
    string filter = CustomerNumberSearch.Text;
    if (string.IsNullOrEmpty(filter))
        _conditions["nro"] = null;
    else
        _conditions["nro"] = string.Format("NRO Like '%{0}%'", filter); //this should be "Begins with" not "Like"
    UpdateFilter();
}

You can modify your database query and let the database do the work more efficiently.
But if you still prefer to do it using C#, you can use LINQ to filter the DataTable :

MainWindow.xaml

<Window>
  <Window.DataContext>
    <ViewModel />
  </Window.DataContext>

  <DataGrid ItemsSource={Binding Data} />
</Window>

ViewModel.cs

public class ViewModel
{
  public DataTable Data { get; set; }

  public ViewModel()
  {
    this.Data = new DataTable();
  }

  // Button ICommand handler
  private void ExecuteGetDataCommand(object param)
  {
    DataTable dataTable = QueryDatabase(); 

    // Filter DataTable using LINQ
    this.Data = dataTable
      .AsEnumerable()
      .Where(row => row["NRO"].ToString().StartsWith("3")
        && row["ACTIVE"].ToString().StartsWith("1"))
      .CopyToDataTable();
  }

  private DataTable QueryDatabase()
  {
    OdbcConnection dbConnection = new OdbcConnection("Driver={Pervasive ODBC Client Interface};ServerName=875;dbq=@DBFS;Uid=Username;Pwd=Password;");
    string strSql = "select NRO,NAME,NAMEA,NAMEB,ADDRESS,POSTA,POSTN,POSTADR,COMPANYN,COUNTRY,ID,ACTIVE from COMPANY";
    dbConnection.Open();
    OdbcDataAdapter dadapter = new OdbcDataAdapter();
    dadapter.SelectCommand = new OdbcCommand(strSql, dbConnection);
    DataTable table = new DataTable("COMPANY");
    dadapter.Fill(table);
    return table;
  }
}

Update

This is the solution based on your posted code. I replaced the TextBox to filter the ACTIVE column with a CheckBox , to add some type safety (to prevent invalid input eg, alphabetical). I also used data binding to connect the DataGrid to the DataTable . You should prefer to use data binding over code-behind and implement as much as possible in XAML:

MainWindow.xaml

<Window x:Class="DB_inspector.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Liinos_inspector"
        mc:Ignorable="d"
        Title="DB database inspector" Height="595.404" Width="1005.571">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="11*"/>
            <RowDefinition Height="553*"/>
        </Grid.RowDefinitions>

        <DataGrid x:Name="DataGrid1" Margin="0,51,0,0" Grid.Row="1"
                  ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MainWindow}}, Path=FilteredData}" />

        <Image Height="41" Margin="847,10,10,502" Width="141" Source="Logo_small.jpg" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Top"/>

        <TextBox x:Name="NameSearch" HorizontalAlignment="Left" Height="23" Margin="77,14,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="409" Grid.Row="1" 
                 TextChanged="OnNameSearchTextBox_TextChanged"/>
        <Button Content="Refrsh" HorizontalAlignment="Left" Height="23" Margin="736,14,0,0" VerticalAlignment="Top" Width="72" Click="Button_Click" Grid.Row="1" Background="#FF51C951" BorderBrush="{x:Null}" Foreground="White"/>
        <CheckBox x:Name="ActiveSearch" HorizontalAlignment="Left" Height="23" Margin="502,14,0,0" Grid.Row="1" VerticalAlignment="Top" Width="66" 
                  IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MainWindow}}, Path=IsActiveFilterValue}" />
        <TextBox x:Name="CustomerNumberSearch" HorizontalAlignment="Left" Height="23" Margin="580,14,0,0" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="82" 
                 TextChanged="OnCustomerNumberSearch_TextChanged"/>

        <ProgressBar x:Name="ProgressBar" HorizontalAlignment="Left" Height="10" Margin="0,1,0,0" VerticalAlignment="Top" Width="998" BorderBrush="{x:Null}" Background="{x:Null}"/>
        <Button Content="Button" HorizontalAlignment="Left" Height="23" Margin="680,14,0,0" Grid.Row="1" VerticalAlignment="Top" Width="51" Click="Button_Click_1"/>

    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
  public static readonly DependencyProperty FilteredDataProperty = DependencyProperty.Register(
    "FilteredData", 
    typeof(DataTable),
    typeof(MainWindow));

  public DataTable FilteredData 
  {
    get { return (DataTable)GetValue(FilteredDataProperty); }
    set { SetValue(FilteredDataProperty, value); }
  }

  public static readonly DependencyProperty IsActiveFilterValueProperty = DependencyProperty.Register(
    "IsActiveFilterValue", 
    typeof(bool),
    typeof(MainWindow), 
    new PropertyMetadata(false, OnIsActiveFilterValueChanged));

  public bool IsActiveFilterValue
  {
    get { return (bool)GetValue(IsActiveFilterValueProperty); }
    set { SetValue(IsActiveFilterValueProperty, value); }
  }

  private DataTable UnfilteredData { get; set; }
  private String NameFilterValue { get; set; }
  private int CustomerNumberFilterValue { get; set; }


  public MainWindow()
  {
    InitializeComponent();
  }

  private void Button_Click(object sender, RoutedEventArgs e)
  {
    string connectionString = "Driver={Pervasive ODBC Client Interface};ServerName=875;dbq=@DBFS;Uid=Username;Pwd=Password;";

    string queryString = "select NRO,NAME,NAMEA,NAMEB,ADDRESS,POSTA,POSTN,POSTADR,COMPANYN,COUNTRY,ID,ACTIVE from COMPANY";

    // using-statement will cleanly close and dispose unmanaged resources i.e. IDisposable instances
    using (OdbcConnection dbConnection = new OdbcConnection(connectionString))
    {    
      dbConnection.Open();
      OdbcDataAdapter dadapter = new OdbcDataAdapter();
      dadapter.SelectCommand = new OdbcCommand(queryString, dbConnection);

      this.UnfilteredData = new DataTable("COMPANY");
      dadapter.Fill(this.UnfilteredData);
      this.FilteredData = this.UnfilteredData;
    }
  }

  private void ApplyFilterOnDataTable()
  {
    // Filter DataTable using LINQ
    this.FilteredData = this.UnfilteredData
      .AsEnumerable()
      .Where(row => 
        string.IsNullOrWhiteSpace(this.NameFilterValue) 
          ? true // Filter criteria is ignored, when input is empty or null
          : row["NAME"].StartsWith(this.NameFilterValue)   
        && this.CustomerNumberFilterValue < 0 
          ? true // Filter criteria is ignored, when value is < 0
          : row["NRO"].ToString().StartsWith(this.CustomerNumberFilterValue.ToString())
        && this.IsActiveFilterValue 
          ? row["ACTIVE"].ToString().Equals("1")
          : true) // Filter criteris is ignored, when CheckBox is unchecked
      .CopyToDataTable();        
  }  

  private void OnNameSearchTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
  {       
    this.NameFilterValue = NameSearch.Text;
    ApplyFilterOnDataTable();  
  }

  private static void OnIsActiveFilterValueChanged(DependencyObject d, 
    DependencyPropertyChangedEventArgs e)
  {
    var _this = d as MainWindow;   
    _this.ApplyFilterOnDataTable();            
  }

  private void OnCustomerNumberSearch_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
  {  
    // Check if input is numeric 
    this.CustomerNumberFilterValue = Int32.TryParse(CustomerNumberSearch.Text, out int number) 
      ? number
      : -1; // Parsing failed -> not a numeric value. -1 will disable this filter criteria
    ApplyFilterOnDataTable();   
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM