DataFilter for WinForms | ComponentOne
C1.Win.DataFilter.4.5.2 Assembly / C1.Win.DataFilter Namespace / C1DataFilter Class
Members

In This Topic
    C1DataFilter Class
    In This Topic
    The C1DataFilter lets users filter data based on criteria's. It can be bound to any data-aware control and multiple conditional filtering could be applied.
    Object Model
    C1DataFilter Class
    Syntax
    'Declaration
     
    Public Class C1DataFilter 
       Inherits C1.Framework.XViewHost
    public class C1DataFilter : C1.Framework.XViewHost 
    Remarks
    The C1DataFilter class allows you to define the behavior of the DataFilter control with the help of following options:
    • Generate the filters automatically by setting the AutoGenerateFilters property to true. This property must be set before setting the DataSource property of the DataFilter control as these filters are created depending on the type of fields present in the DataSource.
    • Bind the DataFilter control to the datasource by setting the DataSource property. The C1DataFilter control and the data-aware control which is to be filtered should have the same datasource.
    • Enable/disable the automatic filtering of the data aware control as soon as the filter value is changed, by setting the AutoApply property to true/false respectively. When the AutoApply property is set to false, you can use the ApplyFilter or ApplyFilterAsync methods to apply the filters to the datasource.
    • Get the collection of filters with the help of Filters property.
    • Set the header text of the DataFilter control using the HeaderText property and determine its visibility with the help of ShowHeader property.
    • Customize the appearance of the DataFilter control and its elements with the help of C1DataFilter’s Styles property.
    • Retrieve the filtered view the C1DataFilter.DataSource with the help of the View property.
    • Save/Load the current filter expressions to/from an XML file using the SaveFilterExpression and LoadFilterExpression methods.
    Example

    private void InitializeDataFilter()
    {

     //Initialize the DataFilter Control
     c1DataFilter1 = new C1DataFilter();

     c1DataFilter1.Dock = DockStyle.Fill;

     splitContainer1.Panel1.Controls.Add(c1DataFilter1);

     //Set the datasource of the DataGridView
     dataGridView1.DataSource = _carsTable;

     //Setting AutoGenerateFilters to true generates the filters automatically.
     c1DataFilter1.AutoGenerateFilters = true;

     //Subscribe to the FilterAutoGenerating event to modify the automatically generated filters.
     c1DataFilter1.FilterAutoGenerating += C1DataFilter1_FilterAutoGenerating;

     //Set the datasource of C1DataFilter equal to the datasource assigned to DataGridView
     c1DataFilter1.DataSource = _carsTable;

     //Automatically applies the filter to the DataSource after the filter value is changed.
     c1DataFilter1.AutoApply = true;

     //Shows the DataFilter header
     c1DataFilter1.ShowHeader = true;

     //Sets the header text of the DataFilter control
     c1DataFilter1.HeaderText = "Refine by";

     //Styling the DataFilter and its elements
     c1DataFilter1.Styles.Common.Border = new C1.Framework.Thickness(5);

     c1DataFilter1.Styles.Header.Font = new Font(FontFamily.GenericSansSerif, 9f, FontStyle.Bold);

     c1DataFilter1.Styles.FilterCaption.BackColor = Color.LightCyan;

    }

    private void C1DataFilter1_FilterAutoGenerating(object sender, C1.DataFilter.FilterAutoGeneratingEventArgs e)

    {

     switch (e.Property.Name)

     {

      //Set the checklist items for Brand filter
     case "Brand":

      var brandFilter = (C1.Win.DataFilter.ChecklistFilter) e.Filter;

      brandFilter.ItemsSource = _carsTable;

      brandFilter.ValueMemberPath = "Brand";

      brandFilter.SelectAll();

      break;

      //Set the checklist items for Category filter
     case "Category":

      var categoryFilter = (C1.Win.DataFilter.ChecklistFilter) e.Filter;

      categoryFilter.ItemsSource = _carsTable;

      categoryFilter.ValueMemberPath = "Category";

      categoryFilter.SelectAll();

      break;

      //Set the minimum/maximum value for the Price filter
     case "Price":

      var priceFilter = (C1.Win.DataFilter.RangeFilter) e.Filter;

      priceFilter.Maximum = _carsTable.AsEnumerable().Max(x = >x.Field < double > ("Price"));

      priceFilter.Minimum = _carsTable.AsEnumerable().Min(x = >x.Field < double > ("Price"));

      priceFilter.Increment = 1000;

      priceFilter.Digits = 0;

      break;

      //Cancels the creation of all other filters
     default:

      e.Cancel = true;

      break;

     }

    }

    private void SaveFilterExpression()

    {

     //Saves the current filter expressions to an XML file
     c1DataFilter1.SaveFilterExpression("SavedFilters.xml");

    }

    private void LoadFilterExpression()

    {

     //Loads the current filter expressions from an XML file
     c1DataFilter1.LoadFilterExpression("SavedFilters.xml");

    }

    Inheritance Hierarchy

    System.Object
       System.MarshalByRefObject
          System.ComponentModel.Component
             System.Windows.Forms.Control
                C1.Framework.XViewHost
                   C1.Win.DataFilter.C1DataFilter

    See Also