This quick start will guide you through the steps of adding DataFilter and DataGridView controls to an application, binding DataGridView to a data source and setting the properties of controls.
The following image shows filtered values in DataGridView on the basis of filters applied in the DataFilter control.
Complete the steps given below to see how the DataFilter control appears after data binding and setting properties.
C1NWindDataSet.xsd is added to your project and DataSource property of the DataGridView is set to carsBindingSource.
Visual Basic |
Copy Code
|
---|---|
'Subscribe the FilterAutoGenerating event of DataFilter to define criterias for the filters.
c1DataFilter1.FilterAutoGenerating = (c1DataFilter1.FilterAutoGenerating + C1DataFilter1_FilterAutoGenerating)
|
C# |
Copy Code
|
---|---|
//Subscribe the FilterAutoGenerating event of DataFilter to define criterias for the filters.
c1DataFilter1.FilterAutoGenerating += C1DataFilter1_FilterAutoGenerating;
|
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.
VIsual Basic |
Copy Code
|
---|---|
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 = c1NWindDataSet.Cars brandFilter.ValueMemberPath = "Brand" brandFilter.SelectAll Case "Category" Dim categoryFilter = CType(e.Filter,C1.Win.DataFilter.ChecklistFilter) categoryFilter.ItemsSource = c1NWindDataSet.Cars categoryFilter.ValueMemberPath = "Category" categoryFilter.SelectAll Case "Price" Dim priceFilter = CType(e.Filter,C1.Win.DataFilter.RangeFilter) priceFilter.Maximum = c1NWindDataSet.Cars.AsEnumerable.Max(() => { }, x.Field(Of Double)("Price")) priceFilter.Minimum = c1NWindDataSet.Cars.AsEnumerable.Min(() => { }, x.Field(Of Double)("Price")) priceFilter.Increment = 1000 priceFilter.Digits = 0 Case Else e.Cancel = true End Select End Sub |
C# |
Copy Code
|
---|---|
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 = c1NWindDataSet.Cars; 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 = c1NWindDataSet.Cars; 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 = c1NWindDataSet.Cars.AsEnumerable().Max(x => x.Field<double>("Price")); priceFilter.Minimum = c1NWindDataSet.Cars.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; } } |
VIsual Basic |
Copy Code
|
---|---|
'Set the datasource of C1DataFilter equal to the datasource assigned to DataGridView
c1DataFilter1.DataSource = carsBindingSource
|
C# |
Copy Code
|
---|---|
//Set the datasource of C1DataFilter equal to the datasource assigned to DataGridView
c1DataFilter1.DataSource = carsBindingSource;
|
Run the application and observe that filters are automatically applied to the rendered grid because the AutoApply property of the DataFilter control is set to true by default. Use interactive UI of DataFilter to change the filter values and observe how the grid gets filtered.
The following image shows filtered values in DataGridView on the basis of filters applied in the DataFilter control.
Complete the steps given below to see how the DataFilter control appears after data binding and setting properties.
Create a new Windows Forms application.
Initialize the DataFilter and DataGridView objects using the following code:
C# |
Copy Code
|
---|---|
//Initialize Objects this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.c1DataFilter1 = new C1.Win.DataFilter.C1DataFilter(); |
Set the DataGrid and DataFilter properties using the following code.
C# |
Copy Code
|
---|---|
//DataGrid this.dataGridView1.AutoGenerateColumns = false; this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.brandDataGridViewTextBoxColumn, this.modelDataGridViewTextBoxColumn, this.categoryDataGridViewTextBoxColumn, this.priceDataGridViewTextBoxColumn}); this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Right; this.dataGridView1.Location = new System.Drawing.Point(310, 0); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.Size = new System.Drawing.Size(446, 575); this.dataGridView1.TabIndex = 3; this.Controls.Add(this.dataGridView1); //DataFilter this.c1DataFilter1.AutoGenerateFilters = true; this.c1DataFilter1.Dock = System.Windows.Forms.DockStyle.Left; this.c1DataFilter1.Location = new System.Drawing.Point(0, 0); this.c1DataFilter1.Name = "c1DataFilter1"; this.c1DataFilter1.Size = new System.Drawing.Size(304, 575); this.c1DataFilter1.TabIndex = 0; this.c1DataFilter1.Text = "c1DataFilter1"; this.c1DataFilter1.Dock = DockStyle.Left; // this.c1DataFilter1.Anchor=AnchorStyles. this.Controls.Add(this.c1DataFilter1); |
Bind the DataGridView to a data source using the following code:
C# |
Copy Code
|
---|---|
//Data Binding Code dt = new DataTable(); string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"ComponentOne Samples\Common\C1NWind.mdb") + ";"; OleDbConnection conn = new OleDbConnection(connectionString); OleDbDataAdapter adapter = new OleDbDataAdapter("Select Brand, Model, Category, Price from Cars", conn); adapter.Fill(dt); this.dataGridView1.DataSource = dt; |
C# |
Copy Code
|
---|---|
//Subscribe the FilterAutoGenerating event of DataFilter to define criterias for the filters.
c1DataFilter1.FilterAutoGenerating += C1DataFilter1_FilterAutoGenerating;
|
C# |
Copy Code
|
---|---|
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.SelectAll(); break; //Set the checklist items for Category filter case "Category": var categoryFilter = (C1.Win.DataFilter.ChecklistFilter)e.Filter; categoryFilter.SelectAll(); break; //Set the minimum/maximum value for the Price filter case "Price": var priceFilter = (C1.Win.DataFilter.RangeFilter)e.Filter; priceFilter.Maximum = dt.AsEnumerable().Max(x => x.Field<double>("Price")); priceFilter.Minimum = dt.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; } |
C# |
Copy Code
|
---|---|
//Set DataSource for DataFilter
c1DataFilter1.DataSource = dt;
|