Search code examples
c#datagridcomponentone

Set default filter in C1 DataGrid


I have a ComponentOne DataGrid with a filter row on top. Filtering works fine. I need to have filter of a checkbox column to be set by default. In other words, when grid is initially shown the filter should be set to show just checked (active) rows.

There is a Filter keyword but I cannot figure out how to use it in XAML.

    <c1:C1DataGrid.TopRows>
        <c1:DataGridFilterRow/>
    </c1:C1DataGrid.TopRows>

    <c1:C1DataGrid.Columns>
        <c1:DataGridBoundColumn Header="ID" Binding="{Binding Id}" />
        <c1:DataGridBoundColumn Header="PROJECT/BAU" Binding="{Binding Project}" />
        <c1:DataGridBoundColumn Header="DESCRIPTION" Binding="{Binding Description}" />
        <c1:DataGridCheckBoxColumn Header="ACTIVE" Binding="{Binding IsActive}" />
    </c1:C1DataGrid.Columns>

Solution

  • C1DataGrid has this FilterBy method. As specified here, this method takes column (column you wish to filter) and FilterState to be applied. Now, before you use this method, you need to create your own DatagridFilterState by setting its FilterInfo.

    Something like the following should filter your checkbox type column to show only checked values.

     List<DataGridFilterInfo> filterInfoList = new List<DataGridFilterInfo>();
     filterInfoList.Add(new DataGridFilterInfo() { FilterOperation = DataGridFilterOperation.Equal, FilterType = DataGridFilterType.CheckBox, Value = true });
     filterState = new DataGridFilterState();
     filterState.FilterInfo = filterInfoList;
    
     grid.FilterBy(grid.Columns["checkboxColumn"], filterState);