DataFilter > クイックスタート |
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 |
コードのコピー
|
---|---|
'DataFilterのFilterAutoGeneratingイベントを購読して、フィルターの基準を定義します
c1DataFilter1.FilterAutoGenerating = (c1DataFilter1.FilterAutoGenerating + C1DataFilter1_FilterAutoGenerating)
|
C# |
コードのコピー
|
---|---|
//DataFilterのFilterAutoGeneratingイベントを購読して、フィルターの基準を定義します
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 |
コードのコピー
|
---|---|
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# |
コードのコピー
|
---|---|
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 = c1NWindDataSet.Cars; brandFilter.ValueMemberPath = "Brand"; brandFilter.SelectAll(); break; //Categoryフィルターのチェックリスト項目を設定します case "Category": var categoryFilter = (C1.Win.DataFilter.ChecklistFilter)e.Filter; categoryFilter.ItemsSource = c1NWindDataSet.Cars; categoryFilter.ValueMemberPath = "Category"; categoryFilter.SelectAll(); break; //Priceフィルターの最小値・最大値を設定します 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; //すべてのフィルターの作成をキャンセルします default: e.Cancel = true; break; } } |
VIsual Basic |
コードのコピー
|
---|---|
'C1DataFilterのデータソースをDataGridViewに割り当てられているデータソースと同じ値に設定します
c1DataFilter1.DataSource = carsBindingSource
|
C# |
コードのコピー
|
---|---|
//C1DataFilterのデータソースを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# |
コードのコピー
|
---|---|
//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# |
コードのコピー
|
---|---|
//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# |
コードのコピー
|
---|---|
//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# |
コードのコピー
|
---|---|
//フィルターの条件を定義するには、DataFilter の FilterAutoGenerating イベントをサブスクライブします。
c1DataFilter1.FilterAutoGenerating += C1DataFilter1_FilterAutoGenerating;
|
C# |
コードのコピー
|
---|---|
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.SelectAll(); break; //Category フィルタのチェックリスト項目を設定します case "Category": var categoryFilter = (C1.Win.DataFilter.ChecklistFilter)e.Filter; categoryFilter.SelectAll(); break; //Price フィルタの最小値/最大値を設定します 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; //他のすべてのフィルタの作成をキャンセルします default: e.Cancel = true; break; } |
C# |
コードのコピー
|
---|---|
//DataFilter の DataSource を設定します
c1DataFilter1.DataSource = dt;
|