Welcome to the Xceed Community | Help
Community Search  
More Search Options

Extending the ContainsFilterCriterion

Sort Posts: Previous Next
  •  08-25-2011, 3:01 PM Post no. 30929

    Extending the ContainsFilterCriterion

    If you have a Column that is bound to a Business Object, and then you wish to display a property of that Object and want to filter the data on another property of that object, you might want to create your own custom object criterion. I have created a sample class that you can use in case you wish to filter on a string property of that object. We will extend the ContainsFilterCriterion since we are supplying the FilterCriterion with a string and looking for instances of that string in other string values.

    We will then override the IsMatch(object value) method where we can choose which property of that object we wish to filter on. In this sample, we wish to search for instance of searchedValue in the ChildProperty property of the object of type Child.

    C#

    -------
    public class CustomObjectCriterion : ContainsFilterCriterion
        {
            public CustomObjectCriterion()
                : base()
            {
            }

            public CustomObjectCriterion(object value)
                : base(value)
            {
            }

            public override bool IsMatch(object value)
            {
                if (value == null)
                    return false;

                if (this.Value == null)
                    return true;

                string searchedValue = this.Value.ToString().ToLower();

                if (searchedValue.Length == 0)
                    return true;

                string strValue = value.ToString().ToLower();

                return (value as Child).ChildProperty.Contains(searchedValue);
            }
        }

    -------

     

    Visual Basic.NET

    -------

    Public Class CustomObjectCriterion
        Inherits ContainsFilterCriterion
        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(value As Object)
            MyBase.New(value)
        End Sub

        Public Overrides Function IsMatch(value As Object) As Boolean
            If value Is Nothing Then
                Return False
            End If

            If Me.Value Is Nothing Then
                Return True
            End If

            Dim searchedValue As String = Me.Value.ToString().ToLower()

            If searchedValue.Length = 0 Then
                Return True
            End If

            Dim strValue As String = value.ToString().ToLower()

            Return TryCast(value, Child).ChildProperty.Contains(searchedValue)
        End Function
    End Class

    -------

     

     


    Marc

    Developer in Technical Support
    Xceed - Multi-talented components - http://xceed.com
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.