[]
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:
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.
To bind the DataFilter control with a custom collection, follow these steps:
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 Namespacenamespace 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;
}
}
}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.
Bind DataGridView with data source using the following code:
'_carList is a global variable of type List<Car>
_carList = Car.GetList
dataGridView1.DataSource = _carList//_carList is a global variable of type List<Car>
_carList = Car.GetList();
dataGridView1.DataSource = _carList;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.
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 is a global variable of type List<Car>
_carList = Car.GetList
dataGridView1.DataSource = _carListc1DataFilter1.AutoGenerateFilters = true;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;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 Subprivate 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 = _carList;
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 = _carList;
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 = _carList.AsEnumerable().Max(x => x.Price);
priceFilter.Minimum = _carList.AsEnumerable().Min(x => x.Price);
priceFilter.Increment = 1000;
priceFilter.Digits = 0;
break;
//Cancels the creation of all other filters
default:
e.Cancel = true;
break;
}
}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;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 Subprivate 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\vx.x\DataFilter\CS