Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.     /// <summary>
  2.     /// The class extends the functionality of a DataGridView with filtering
  3.     /// </summary>
  4.     [Serializable()]
  5.     [ToolboxBitmap(typeof(DataGridView))]
  6.     public partial class FilterableDataGridView : DataGridView
  7.     {
  8.         /// <summary>
  9.         /// A collection to store the filters
  10.         /// </summary>
  11.         /// <remarks>
  12.         /// ObservableCollection&lt;T&gt; requires .NET 4.0
  13.         /// </remarks>
  14.         private ObservableCollection<FilterItem> _filters;
  15.  
  16.         /// <summary>
  17.         /// Property to get or set the collection of the filters
  18.         /// </summary>
  19.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  20.         public ObservableCollection<FilterItem> Filters
  21.         {
  22.             get
  23.             {
  24.                 return _filters;
  25.             }
  26.             set
  27.             {
  28.                 _filters = value;
  29.  
  30.                 if (_filters != null)
  31.                 {
  32.                     // Subscribe to the CollectionChanged event, so the filtering can be done automatically
  33.                     _filters.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_filters_CollectionChanged);
  34.                 }
  35.             }
  36.         }
  37.  
  38.         /// <summary>
  39.         /// The event handler of the filters CollectionChanged event,
  40.         /// so the filtering can be automatic when the collection changes
  41.         /// </summary>
  42.         /// <param name="sender">sender</param>
  43.         /// <param name="e">Event arguments</param>
  44.         void _filters_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  45.         {
  46.             //If a new element is added
  47.             if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
  48.             {
  49.                 foreach (FilterItem fi in e.NewItems)
  50.                 {
  51.                     //subscribe to its event, so filtering will be done automatically every time when the filter changes
  52.                     fi.FilterChanged += Filter;
  53.                 }
  54.             }
  55.             //If the filter is removed
  56.             else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
  57.             {
  58.                 foreach (FilterItem fi in e.OldItems)
  59.                 {
  60.                     //unsubscribe from its event
  61.                     fi.FilterChanged -= Filter;
  62.                 }
  63.             }
  64.  
  65.             //Finally filter the list
  66.             Filter();
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Constructor
  71.         /// </summary>
  72.         public FilterableDataGridView()
  73.         {
  74.             InitializeComponent();
  75.  
  76.             Filters = new ObservableCollection<FilterItem>();
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Constructor
  81.         /// </summary>
  82.         /// <param name="container"></param>
  83.         public FilterableDataGridView(IContainer container)
  84.         {
  85.             container.Add(this);
  86.  
  87.             InitializeComponent();
  88.  
  89.             Filters = new ObservableCollection<FilterItem>();
  90.         }
  91.  
  92.         /// <summary>
  93.         /// Method to filter the DataGridView
  94.         /// It gets called whenever the filters collection changes or a filter changes.
  95.         /// Explicit call is possible, however not necessary.
  96.         /// </summary>
  97.         public void Filter()
  98.         {
  99.             //Set the selected cell to null, so every cell(/row) can be hidden
  100.             this.CurrentCell = null;
  101.             //Check every row in the DataGridView
  102.             foreach (DataGridViewRow row in Rows)
  103.             {
  104.  
  105.                 bool visible = true;
  106.                 //Check every FilterItem
  107.                 foreach (FilterItem fi in _filters)
  108.                 {
  109.                     //The char used to delimit the columns from each other
  110.                     // it is also used to denote an OR relation ship between the filter texts
  111.                     char c = '|';
  112.                     //Split the string to column names
  113.                     List<string> columns = new List<string>(fi.FilterColumn.Split(c));
  114.                     List<string> filters = new List<string>(fi.Filter.Split(c));
  115.                     bool atLeastOneContains = false;
  116.  
  117.                     //Check every columns
  118.                     foreach (string column in columns)
  119.                     {
  120.                         foreach (string filter in filters)
  121.                         {
  122.                             if (row.Cells[column].Value != null && row.Cells[column].Value.ToString().ToUpper().Contains(filter.ToUpper()))
  123.                             {
  124.                                 //If the column contains any of the filter texts, the filter is satisfied
  125.                                 atLeastOneContains = true;
  126.                                 break;
  127.                             }
  128.                         }
  129.                         if (atLeastOneContains)
  130.                         {
  131.                             //If the column contains the filter text, the filter is satisfied
  132.                             break;
  133.                         }
  134.                     }
  135.                     //If none of the columns contain the text, the row can't be visible
  136.                     if (!atLeastOneContains)
  137.                     {
  138.                         visible = false;
  139.                         break;
  140.                     }
  141.                 }
  142.                 //Set the Visible property the Row
  143.                 row.Visible = visible;
  144.             }
  145.         }
  146.     }
  147.  
  148.