FlexGrid for UWP
フルテキストフィルタを使用してデータの検索
C1FlexGrid の使い方 > データのフィルタ処理 > フルテキストフィルタを使用してデータの検索

Searching a grid with huge data source can be a tedious task even in a grid that supports column filtering and sorting. To make this task easier, FlexGrid supports searching the entire data source connected to the grid through full text filtering. To apply full text filtering, you need to use BindingPaths property of the FullTextFilterCondition class, which specifies the data column(s) that will participate in the full text search filtering. For searching, you can use an editor control to act as a search box where user provides the input for obtaining the search results. You need to handle an event depending on whether you want to search as the user types in or once the input has completed. In that event, you can invoke ApplyFullTextFilter method of the C1CollectionView class which accepts input text and instance of the FullTextFilterCondition class as its parameters.

In addition, the FullTextFilterCondition class provides conditions to perform full text search filtering in the control. These conditions can be applied to the user input for customizing the filtering behavior using the MatchCaseMatchWholeWord and TreatSpacesAsAndOperator properties.

In the following example, we used a TextBox as a search box and invoked the ApplyFullTextFilter method in TextChanged event of the TextBox to implement full text search filtering in FlexGrid.

  1. Add C1FlexGrid and TextBox control to the XAML designer and add five columns to the grid to display data using the following code:
    XAML
    コードのコピー
    <Grid>
        <TextBox x:Name="filterTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" 
                 TextChanged="filterTextBox_TextChanged" Margin="0,20,0,0" Width="250"/>
        <Custom:C1FlexGrid x:Name="c1FlexGrid1" HorizontalAlignment="Left" 
                           AutoGenerateColumns="false" ColumnHeaderForeground="Black" 
                           Margin="0,60,0,0">
            <Custom:C1FlexGrid.Columns>
                <Custom:Column Binding="{Binding ID, Mode=TwoWay}"/>
                <Custom:Column Binding="{Binding Name, Mode=TwoWay}" Width="*"/>
                <Custom:Column Binding="{Binding Email, Mode=TwoWay}" Width="*" />
                <Custom:Column Binding="{Binding OrderDate, Mode=TwoWay}" Width="*"/>
                <Custom:Column Binding="{Binding OrderTotal, Mode=TwoWay}" Width="*"/>
            </Custom:C1FlexGrid.Columns>
        </Custom:C1FlexGrid>
    </Grid>
    
  2. Switch to the Code view and create a class, Customers, to add data in the FlexGrid.
    Public Class Customer
        Private _id As Integer
        Private _name, _email As String
        Private _OrderDate As DateTime
        Private _orderTotal As Double
            
        Shared _rnd As Random = New Random()
        Shared _firstNames As String() = "Andy|Ben|Charlie|Dan|Ed|Fred|Herb|Elena|Stefan|Gina".Split("|"c)
        Shared _lastNames As String() = "Ambers|Bishop|Cole|Danson|Evers|Frommer|Heath|Salvatore|Spencer|Rodriguez".Split("|"c)
        Shared _emailServers As String() = "gmail|yahoo|outlook|aol".Split("|"c)
    
        Public Sub New()
            Me.New(_rnd.[Next]())
        End Sub
    
        Public Sub New(ByVal id As Integer)
            ID = id
            Name = GetName()
            Email = String.Format("{0}@{1}.com", (Name.Substring(0, 1)).ToLower(), GetString(_emailServers))
            OrderDate = DateTime.Today.AddDays(-_rnd.[Next](1, 365)).AddHours(_rnd.[Next](0, 24)).AddMinutes(
                                            _rnd.[Next](0, 60))
            OrderTotal = Math.Round(_rnd.NextDouble() * 10000.00, 2)
        End Sub
    
        Public Property ID As Integer
            Get
                Return _id
            End Get
            Set(ByVal value As Integer)
    
                If value <> _id Then
                    _id = value
                End If
            End Set
        End Property
    
        Public Property Name As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
    
                If value <> _name Then
                    _name = value
                End If
            End Set
        End Property
    
        Public Property Email As String
            Get
                Return _email
            End Get
            Set(ByVal value As String)
    
                If value <> _email Then
                    _email = value
                End If
            End Set
        End Property
    
        Public Property OrderDate As DateTime
            Get
                Return _OrderDate
            End Get
            Set(ByVal value As DateTime)
    
                If value <> _OrderDate Then
                    _OrderDate = value
                End If
            End Set
        End Property
    
        Public Property OrderTotal As Double
            Get
                Return _orderTotal
            End Get
            Set(ByVal value As Double)
    
                If value <> _orderTotal Then
                    _orderTotal = value
                End If
            End Set
        End Property
    
        Private Shared Function GetString(ByVal arr As String()) As String
            Return arr(_rnd.[Next](arr.Length))
        End Function
    
        Private Shared Function GetName() As String
            Return String.Format("{0} {1}", GetString(_firstNames), GetString(_lastNames))
        End Function
    
        Public Shared Function GetCustomerList(ByVal count As Integer) As ObservableCollection(Of Customer)
            Dim list = New ObservableCollection(Of Customer)()
    
            For i As Integer = 0 To count - 1
                list.Add(New Customer(i))
            Next
    
            Return list
        End Function
    End Class
    
    public class Customer
    {
        //フィールド
        int _id;
        string _name, _email;
        DateTime _OrderDate;
        double _orderTotal;
    
        //データジェネレータ
        static Random _rnd = new Random();
        static string[] _firstNames = "Andy|Ben|Charlie|Dan|Eddy|Fred|Herb|Elena|Stefan|Gina".Split('|');
        static string[] _lastNames = "Ambers|Bishop|Cole|Danson|Evers|Frommer|Heath|Salvatore|Spencer|Rodriguez".Split('|');
        static string[] _emailServers = "gmail|yahoo|outlook|aol".Split('|');
    
        public Customer()
            : this(_rnd.Next())
        {
        }
        public Customer(int id)
        {
            ID = id;
            Name = GetName();
            Email = string.Format("{0}@{1}.com", (Name.Substring(0, 3)).ToLower(),
                    GetString(_emailServers));
            OrderDate = DateTime.Today.AddDays(-_rnd.Next(1, 365)).AddHours(
                        _rnd.Next(0, 24)).AddMinutes(_rnd.Next(0, 60));
            OrderTotal = Math.Round(_rnd.NextDouble() * 10000.00, 2);
        }
        //オブジェクトモデル
        public int ID
        {
            get { return _id; }
            set
            {
                if (value != _id)
                {
                    _id = value;
                }
            }
        }
        public string Name
        {
            get { return _name; }
            set
            {
                if (value != _name)
                {
                    _name = value;
                }
            }
        }
        public string Email
        {
            get { return _email; }
            set
            {
                if (value != _email)
                {
                    _email = value;
                }
            }
        }
        public DateTime OrderDate
        {
            get { return _OrderDate; }
            set
            {
                if (value != _OrderDate)
                {
                    _OrderDate = value;
                }
            }
        }
    
        public double OrderTotal
        {
            get { return _orderTotal; }
            set
            {
                if (value != _orderTotal)
                {
                    _orderTotal = value;
                }
            }
        }
    
        // ** ユーティリティ
        static string GetString(string[] arr)
        {
            return arr[_rnd.Next(arr.Length)];
        }
        static string GetName()
        {
            return string.Format("{0} {1}", GetString(_firstNames), GetString(_lastNames));
        }
    
        public static ObservableCollection<Customer> GetCustomerList(int count)
        {
            var list = new ObservableCollection<Customer>();
            for (int i = 0; i < count; i++)
            {
                list.Add(new Customer(i));
            }
            return list;
        }
    }
    
  3. Add the following code to filter data using full text filtering in the FlexGrid control.
    Public NotInheritable Partial Class MainPage
        Inherits Page
    
        Private cv As C1CollectionView
        Private FilterCondition As FullTextFilterCondition
    
        Public Sub New()
            Me.InitializeComponent()
            Dim list = New ObservableCollection(Of Customer)()
    
            For i As Integer = 0 To 100 - 1
                list.Add(New Customer())
            Next
    
            cv = New C1CollectionView(list)
            c1FlexGrid1.ItemsSource = cv
            FilterCondition = New FullTextFilterCondition()
            FilterCondition.BindingPaths = New List(Of String)() From {
                "ID",
                "Name",
                "OrderDate",
                "OrderTotal"
            }
            FilterCondition.MatchCase = True
        End Sub
    
        Private Sub filterTextBox_TextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
            cv.ApplyFullTextFilter(filterTextBox.Text, FilterCondition)
        End Sub
    End Class
    
    public sealed partial class MainPage : Page
    {
        C1CollectionView cv;
        FullTextFilterCondition FilterCondition;
        public MainPage()
        {
            this.InitializeComponent();
    
            var list = new ObservableCollection<Customer>();
            for (int i = 0; i < 100; i++)
                list.Add(new Customer());
    
            // リストからC1CollectionViewを作成します
            cv = new C1CollectionView(list);
            
            c1FlexGrid1.ItemsSource = cv;
    
            FilterCondition = new FullTextFilterCondition();
            FilterCondition.BindingPaths = new List<string>() { "ID", "Name",
                        "OrderDate", "OrderTotal" };
            
            // FullTextFilterConditionを更新します
            FilterCondition.MatchCase = true;
        }
        void filterTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // FullTextFilterを適用します
            cv.ApplyFullTextFilter(filterTextBox.Text, FilterCondition);
        }
    }