• Understanding Filter Conditions [Janus GridEX WinForms Control v3.5 for .NET]

    GridEXFilterCondition class allows developers to specify simple or complex criteria to be evaluated in rows of a table. A GridEXFilterCondition can be a simple condition that compares only one column with a value using the operator specified or a composite condition that is a collection of simple conditions linked by logical operators like And, Or and Xor. Filter conditions can be used in all the data sources supported by the control even if the data source doesn't support filtering like when using an IList.



    Typically, as its name indicates, filter conditions are used to filter rows in a GridEX control. However, you can also use a GridEXFilterCondition to specify complex criteria in a GridEXFormatCondition, as the criteria for the Find and FindAll methods or as the criteria to include the value for a row when calculating totals in a GridEXGroupHeaderTotal.



    To create a simple condition, you just need to specify the column the operator and the value that will be compared with the value of the column in a row. For example, to show only the rows where the column ‘LastName' is equal to ‘Smith' the code will be as follows:



    Dim lastNameColumn As GridEXColumn

    'Get a reference of the column to be compared

    lastNameColumn = GridEX1.RootTable.Columns("LastName")

    'Create the condition

    Dim condition As New GridEXFilterCondition(lastNameColumn, ConditionOperator.Equal, "Smith")

    'Assign the condition to the table to filter the records

    GridEX1.RootTable.FilterCondition = condition



    Note: Once a filter condition is assigned to the FilterCondition property of a table, you can not make changes to it. To change a filter condition being used, create a clone of the filter condition, change the clone and assign it again to the FilterCondition property.



    To create compound conditions, you need to create an empty condition and then add each of the simple conditions to it using the AddCondition method and specifying, if needed, the logical operator that will be used to link both conditions. . For example, to show only the rows with last name ‘Smith' and that were born in 1970 or after, the code will be as follows:



    Dim column As GridEXColumn

    Dim lastNameCondition As GridEXFilterCondition

    Dim birthDateCondition As GridEXFilterCondition

    Dim compositeCondition As GridEXFilterCondition

    'creating the condition for LastName

    'get a reference of the column to be compared

    column = GridEX1.RootTable.Columns("LastName")

    lastNameCondition = New GridEXFilterCondition(column, ConditionOperator.Equal, "Smith")

    'creating the condition for BirthDate

    'get a reference of the column to be compared

    column = GridEX1.RootTable.Columns("BirthDate")

    birthDateCondition = New GridEXFilterCondition(column, ConditionOperator.GreaterThanOrEqualTo, New DateTime(1970, 1, 1))

    'creating the composite condition

    compositeCondition = New GridEXFilterCondition()

    'adding the LastName condition

    compositeCondition.AddCondition(lastNameCondition)

    'adding the BirthDate condition using And as logical operator

    compositeCondition.AddCondition(LogicalOperator.And, birthDateCondition)

    'assign the composite condition to the
    ‘table to filter the records

    GridEX1.RootTable.FilterCondition = compositeCondition



    Composite conditions can also be part of another condition, letting you define groups of conditions and have total control over the precedence of the evaluations. For instance, to filter the grid to show only the orders that are paid made by the employees 1 and 5, the code will be as follows:



    Dim column As GridEXColumn

    Dim paidCondition As GridEXFilterCondition

    Dim employee1Condition As GridEXFilterCondition

    Dim employee5Condition As GridEXFilterCondition

    Dim mainCondition As GridEXFilterCondition

    Dim groupCondition As GridEXFilterCondition

    'creating the condition for paid

    column = GridEX1.RootTable.Columns("Paid")

    paidCondition = New GridEXFilterCondition(column, ConditionOperator.Equal, True)

    'creating the condition for employee = 1

    column = GridEX1.RootTable.Columns("Employee")

    employee1Condition = New GridEXFilterCondition(column, ConditionOperator.Equal, 1)

    'creating the condition for employee = 5

    column = GridEX1.RootTable.Columns("Employee")

    employee5Condition = New GridEXFilterCondition(column, ConditionOperator.Equal, 5)

    mainCondition = New GridEXFilterCondition()

    'creating the first group and adding it to the main condition

    groupCondition = New GridEXFilterCondition()

    groupCondition.AddCondition(paidCondition)

    groupCondition.AddCondition(LogicalOperator.And, employee1Condition)

    mainCondition.AddCondition(groupCondition)

    'creating the second group and adding it to the main condition

    groupCondition = New GridEXFilterCondition()

    'here we use a clone of the paid condition because

    'a GridEXFilterCondition instance can not be part of

    'two conditions

    groupCondition.AddCondition(paidCondition.Clone())

    groupCondition.AddCondition(LogicalOperator.And, employee5Condition)

    mainCondition.AddCondition(groupCondition)

    'Assign the composite condition to the

    ‘table to filter the records

    GridEX1.RootTable.FilterCondition = mainCondition



    Note: If groups weren't used in this case, we would end up showing the orders that are paid for employee 1 and all the orders (paid and unpaid) for employee 5. Is in these cases where the use of composite conditions having other composite conditions is absolutely necessary.



    When working with hierarchical data sources, you can also filter records in child tables. To filter a child table, you just need to create a filter condition using columns from that table and assign that condition to the FilterCondition property of the child table. When a filter is applied to a child table, the control by default filters the parent records that don't have children records that meet the criteria specified in the filter condition. If you want to filter only the child records without affecting the parent table, set the FilterParentRows property to false.



    For instance, in a hierarchical grid with Orders-OrderDetails tables, to filter the orders and see only the orders where ProductId is equal to 1 in one of their details, apply a filter in the order details as follows:



    Dim detailsTable As GridEXTable

    Dim productColumn As GridEXColumn

    detailsTable = GridEX1.RootTable.ChildTables(0)

    'Get a reference of the column to be compared

    productColumn = detailsTable.Columns("ProductID")

    'Create the condition

    Dim condition As New GridEXFilterCondition(productColumn, _

    ConditionOperator.Equal, 1)

    'use FilterParentRows to filter the orders table too

    detailsTable.FilterParentRows = True

    'Assign the condition to the table to filter the records

    detailsTable.FilterCondition = condition





    Filter Conditions can also be created at design time. To create a filter condition at design time, in the properties window, select the FilterCondition property in a table and click "Custom..." in the drop down list. The filter condition designer condition will appear. In the designer, define the conditions and click OK.



    To be able to define more than one filter conditions at design time for a table, you can use the StoredFilters collection of that table. With this collection, you can add several predefined filters and later assign one of them to the table to filter it. For instance, if you define two stored filters one for orders where Paid is True and one for orders where Paid is False, you can filter the table at design time by simply assigning that stored filter to the FilterCondition property. For instance:



    ‘filtering root table using a stored filter with key "Paid Orders"

    GridEX1.RootTable.FilterCondition = GridEX1.RootTable.StoredFilters("Paid Orders")

    Or

    ‘filtering root table using a stored filter with key "Unpaid Orders"

    GridEX1.RootTable.FilterCondition = GridEX1.RootTable.StoredFilters("Unpaid Orders")



    Finally, to clear the filter in a table you can set the FilterCondition property to null (Nothing in Visual Basic) or call the RemoveFilters method in the GridEX control to clear the filters in all the tables at the same time.

    Source Of Information : Janus v3.5 Help Files for VS 2008


1 comments:

  1. Unknown says:

    This was very helpful! Thank you

Leave a Reply