DataFilter for WinForms
データ連結
DataFilter > データ連結

The DataFilter control can be bound to any data-aware control or to a model/custom collection. You can choose to bind the control using any of the following ways:

Binding with data-aware control

When you bind the DataFilter control with a data-aware control, fields are generated. On the basis of these fields present in the data source, different filters are automatically generated in the DataFilter control. These filters are BoolFilter, RangeFilter, DateRangeFilter and ChecklistFilter. However, the filtering criterias are not generated with these filters, for example, no minimum and maximum values are generated for RangeFilter and empty checklist is generated for ChecklistFilter. So, you need to customize the auto generated filters for defining the filtering criterias using FilterAutoGenerating event of the C1DataFilter class.

Model binding

To bind the DataFilter control with a custom collection, follow these steps:

  1. Define the custom collection
  2. Add and bind DataGridView
  3. Add and configure the DataFilter control

Step 1: Define the custom collection

Create a class named Car which contains a method, GetList, that returns the collection which is to be bound to DataFilter and DataGridView.

Namespace DataFilter_ModelBinding
    
    Public Enum brandNames
        
        Audi = 0
        
        Maruti = 1
        
        Lexus = 2
        
        BMW = 3
        
        Infiniti = 4
        
        Tesla = 5
    End Enum
    Class Car
        Public Property Id As Integer
            Get
            End Get
            Set
            End Set
        End Property
        Public Property Model As String
            Get
            End Get
            Set
            End Set
        End Property
        Public Property Category As String
            Get
            End Get
            Set
            End Set
        End Property
        Public Property Brand As String
            Get
            End Get
            Set
            End Set
        End Property
        Public Property Price As Double
            Get
            End Get
            Set
            End Set
        End Property
        Public Property InStock As Boolean
            Get
            End Get
            Set
            End Set
        End Property
        Public Shared Function GetList() As List(Of Car)
            Dim carList As List(Of Car) = New List(Of Car)
            Dim carModels() As String = New String() {"SLK R172 Cabriolet", "Wraith", "XFR-S I", "CTS III", "ES VI Sedan", "IS III"}
            Dim brands() As String = New String() {"Lexus", "Lexus", "Lexus", "Infiniti", "Audi", "Audi"}
            Dim categories() As String = New String() {"Sports", "Truck", "Sports", "Truck", "Saloon", "Saloon"}
            Dim random As Random = New Random
            Dim i As Integer = 0
            Do While (i < 6)
                carList.Add(New Car)
                i = (i + 1)
            Loop
            Return carList
        End Function
    End Class
End Namespace
namespace DataFilter_ModelBinding
{
    public enum brandNames
        {
          Audi=0,
          Maruti=1,
          Lexus=2,
          BMW=3,
          Infiniti=4,
          Tesla=5
        }
    class Car
    {
        public int Id { get; set; }
        public string Model { get; set; }
        public string Category { get; set; }
        public string Brand { get; set; }
        public double Price { get; set; }
        public bool InStock { get; set; }


        public static List<Car> GetList()
        {
            List<Car> carList = new List<Car>();
            string[] carModels = { "SLK R172 Cabriolet", "Wraith", "XFR-S I", "CTS III",
                                   "ES VI Sedan", "IS III" };
            string[] brands = { "Lexus", "Lexus", "Lexus", "Infiniti", "Audi", "Audi" };
            string[] categories = { "Sports", "Truck", "Sports", "Truck", "Saloon", "Saloon" };
            Random random = new Random();
            for (int i = 0; i < 6; i++)
            {
                carList.Add(new Car() { Id = i + 1, Model = carModels[i],
                    Category = categories[i], Price = Math.Round(random.NextDouble() * 100000, 2),
                    Brand = brands[i], InStock = Convert.ToBoolean(random.Next(0, 2)) });
            }
            return carList;
        }
    }
}

Back to Top

Step 2: Add and bind DataGridView

  1. Drag and drop the MS DataGridView control from the Toolbox onto your form. Set the value of its Location property to 10,10 and Size property to 575, 735.
  2. Bind DataGridView with data source using the following code:
    '_carListは、List<Car>型のグローバル変数です           
    _carList = Car.GetList
    dataGridView1.DataSource = _carList
    
    //_carListは、List<Car>型のグローバル変数です        
    _carList = Car.GetList();
    dataGridView1.DataSource = _carList;
    

Back to Top

Step 3: Add and configure the DataFilter control

  1. Drag and drop the C1DataFilter control from the Toolbox onto your form. Set the value of its Location property to 600,10 and Size property to 290, 735.
  2. Set AutoGenerateFilters property to true using the following code.
    Note that setting AutoGenerateFilters property to true automatically generates the filters depending on the type of the fields present in the data source of DataFilter.
    '_carListは、List<Car>型のグローバル変数です              
    _carList = Car.GetList
    dataGridView1.DataSource = _carList
    
    c1DataFilter1.AutoGenerateFilters = true;
    
  3. To customize the auto generated filters for defining the filtering criterias such as the minimum /maximum values for the RangeFilter and the checklist items for the CheckListFilter, subscribe to the FilterAutoGenerating event of DataFilter class as shown in the following code.
    c1DataFilter1.FilterAutoGenerating = (c1DataFilter1.FilterAutoGenerating + C1DataFilter1_FilterAutoGenerating)
    
    c1DataFilter1.FilterAutoGenerating += C1DataFilter1_FilterAutoGenerating;
    
  4. Add the following code in the event handler of the FilterAutoGenerating event which provides the checklist items for the two filters namely “Brand”, “Category” and sets the maximum and minimum value for the price filter.
    Defining these filters allows you to filter the cars listing by a specific brand, category or price which are the basic criterias used to view a car listing.
    Private Sub C1DataFilter1_FilterAutoGenerating(ByVal sender As Object, ByVal e As C1.DataFilter.FilterAutoGeneratingEventArgs)
            Select Case (e.Property.Name)
                Case "Brand"
                    Dim brandFilter = CType(e.Filter,C1.Win.DataFilter.ChecklistFilter)
                    brandFilter.ItemsSource = _carList
                    brandFilter.ValueMemberPath = "Brand"
                    brandFilter.SelectAll
                Case "Category"
                    Dim categoryFilter = CType(e.Filter,C1.Win.DataFilter.ChecklistFilter)
                    categoryFilter.ItemsSource = _carList
                    categoryFilter.ValueMemberPath = "Category"
                    categoryFilter.SelectAll
                Case "Price"
                    Dim priceFilter = CType(e.Filter,C1.Win.DataFilter.RangeFilter)
                    priceFilter.Maximum = _carList.AsEnumerable.Max(() => {  }, x.Price)
                    priceFilter.Minimum = _carList.AsEnumerable.Min(() => {  }, x.Price)
                    priceFilter.Increment = 1000
                    priceFilter.Digits = 0
                Case Else
                    e.Cancel = true
            End Select
        End Sub
    
    private void C1DataFilter1_FilterAutoGenerating(object sender, C1.DataFilter.FilterAutoGeneratingEventArgs e)
    {
        switch(e.Property.Name)
        {
            //Brandフィルターのチェックリスト項目を設定します
            case "Brand":
                var brandFilter = (C1.Win.DataFilter.ChecklistFilter)e.Filter;
                brandFilter.ItemsSource = _carList;
                brandFilter.ValueMemberPath = "Brand";
                brandFilter.SelectAll();
                break;
            //Categoryフィルターのチェックリスト項目を設定します
            case "Category":
                var categoryFilter = (C1.Win.DataFilter.ChecklistFilter)e.Filter;
                categoryFilter.ItemsSource = _carList;
                categoryFilter.ValueMemberPath = "Category";
                categoryFilter.SelectAll();
                break;
            //Priceフィルターの最小値・最大値を設定します
            case "Price":
                var priceFilter = (C1.Win.DataFilter.RangeFilter)e.Filter;
                priceFilter.Maximum = _carList.AsEnumerable().Max(x => x.Price);
                priceFilter.Minimum = _carList.AsEnumerable().Min(x => x.Price);
                priceFilter.Increment = 1000;
                priceFilter.Digits = 0;
                break;
            //すべてのフィルターの作成をキャンセルします
            default:
                e.Cancel = true;
                break;
        }
    }
    
  5. To update the data of the data-aware control, subscribe to the FilterChanged event of DataFilter as shown in the following code.
    Note that the DataFilter control is bound to a data source which does not support filtering like the BindingList, so using FilterChanged event of the C1DataFilter class updates the data of the data-aware control.
    c1DataFilter1.FilterChanged = (c1DataFilter1.FilterChanged + C1DataFilter1_FilterChanged)
    
    c1DataFilter1.FilterChanged += C1DataFilter1_FilterChanged;
    
  6. Add the following code in the event handler of the FilterChanged event which reassigns data source to the FlexGrid control through View property of the C1DataFilter class which contains the result of filtering the C1DataFilter.DataSource to the DataSource property of FlexGrid.
    Private Sub C1DataFilter1_FilterChanged(ByVal sender As Object, ByVal e As EventArgs)
            dataGridView1.DataSource = c1DataFilter1.View.Cast(Of Car).ToList
    End Sub
    
    private void C1DataFilter1_FilterChanged(object sender, EventArgs e)
    {
        dataGridView1.DataSource=c1DataFilter1.View.Cast<Car>().ToList();
    }
    
Note: DataBinding is also possible in the DataFilter control using the DataEngine library. Please refer the DataFilterAndDataEngine sample in \Documents\ComponentOne Samples\WinForms\v4.5.2\DataFilter\CS

Back to Top