Filtering an Object Data Source on a boolean property

5 posts, 0 answers
  1. EpiqDev
    EpiqDev avatar
    10 posts
    Member since:
    Aug 2012

    Posted 20 Oct 2008 Link to this post

    Hello,

    Here is my situation:

    I have a RadGrid with a List<MyObject> as the datasource. The MyObject class has a property of Deleted on it which is a boolean.

    My requirement is to filter to show only MyObjects in the RadGrid that have a Deleted propery of false. Hiding MyObjects that have a Deleted value of true.

    For some reason I cannot figure out how to filter my RadGrid on the Deleted property.

    Here are some of my attempts:

    RadGrid1.MasterTableView.FilterExpression = "Deleted = false";
    RadGrid1.MasterTableView.FilterExpression = "Deleted = 'false'";
    RadGrid1.MasterTableView.FilterExpression = "Deleted is false";
    RadGrid1.MasterTableView.FilterExpression = "NOT Deleted";

    If anyone knows what I am doing wrong, please point me in the right direction.

    Thanks,

    TMP




     

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 21 Oct 2008 Link to this post

    Hi,

    Try the following filter expression to filter on the basis of  a boolean field.

        RadGrid1.MasterTableView.FilterExpression = "([Deleted]=0) ";
        RadGrid1.Rebind();

    This will return all rows with the Deleted property false.

    Thanks,
    Shinu



  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. EpiqDev
    EpiqDev avatar
    10 posts
    Member since:
    Aug 2012

    Posted 21 Oct 2008 Link to this post

    Thank you for your response, however using that filter did not work for my problem. :(

    Addtionally I also tried using

    RadGrid1.MasterTableView.FilterExpression = "[Deleted] = False";

    after seeing this article in the documentation here.

    I have gone with a workaround of using the FindAll function built into List<T> to filter the List before it hits the radGrid.  So instead of using a filer expression, I use the following code.

    RadGrid1.DataSource = MyObjectList.FindAll(FindOnlyActive)

            /// <summary>     
            /// Finds only active MyObjects.     
            /// </summary>     
            /// <param name="Instance">The instance.</param>     
            /// <returns></returns>     
            private bool FindOnlyActive(MyObject Object)     
            {     
                bool result = true;     
                if (result.Deleted)     
                    result = false;     
        
                return result;     
            }   

    This solution works for me.

    Thanks Again,

    TMP



  5. Kamal
    Kamal avatar
    34 posts
    Member since:
    Jan 2013

    Posted 03 Mar 2015 in reply to EpiqDev Link to this post

    Hello Shinu

    I'm really having hard time setting the default filter. Same as mentioned in the initial post (tried FilterExpression in every way), but I get a yellow screen saying:

    Expression expected

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for
    more information about the error and where it originated in the code.

    Exception Details: Telerik.Web.UI.ParseException: Expression expected

    Source Error:
    Line 310: column.CurrentFilterValue = true.ToString();
    Line 311:
    Line 312: RadGrid1.MasterTableView.Rebind();
    Line 313: }
    Line 314: }

    Source File: c:\PROJECTS\UPM4.0\UserPermissionManager\Applications.aspx.cs    Line: 312



    If I comment this line in PreRender function
    //RadGrid1.MasterTableView.FilterExpression = "([Active] = " + true.ToString() + ") ";

    The page loads, the filter is set as expected. But the grid is not filtered, means all rows(Active/Inactive) are shown. Below is the code blocks for reference. Please help.

    ----------------------------------------- starts - relevant code blocks for reference ---------------------------
            public DataTable BindApplications()
            {
                return objApp.ListApplications().Tables[1];
            }

            private DataTable ApplicationsTable
            {
                get
                {
                    object obj = Session["Applications"];
                    DataTable myDataTable = new DataTable();
                    if (obj != null)
                        myDataTable = (DataTable)obj;
                    else
                        myDataTable = BindApplications();

                    Session["Applications"] = myDataTable;
                    return myDataTable;
                }
            }

            protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
            {
                RadGrid1.DataSource = ApplicationsTable;
                ApplicationsTable.PrimaryKey = new DataColumn[] { ApplicationsTable.Columns["AppID"] };
            }

            protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
            {
                if (e.Item is GridDataItem)
                {
                    //Get the instance of the right type
                    GridDataItem dataBoundItem = e.Item as GridDataItem;

                    //Check the formatting condition
                    if (dataBoundItem["Active"].Text.ToLower() == "true")
                    {
                        dataBoundItem["Active"].Text = "YES";
                        dataBoundItem["Active"].ForeColor = dataBoundItem["AppName"].ForeColor = Color.Green;
                        dataBoundItem["Active"].Font.Bold = dataBoundItem["AppName"].Font.Bold = true;

                    }
                    else
                    {
                        dataBoundItem["Active"].Text = "NO";
                        dataBoundItem["Active"].ForeColor = dataBoundItem["AppName"].ForeColor = Color.Red;
                    }
                }
            }


            protected void RadGrid1_PreRender(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    RadGrid1.MasterTableView.FilterExpression = "([Active] = " + true.ToString() + ") ";
                    GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("Active");
                    column.CurrentFilterFunction = GridKnownFunction.EqualTo;
                    column.CurrentFilterValue = true.ToString();

                    RadGrid1.MasterTableView.Rebind();
                }
            }
    ----------------------------------------- ends - relevant code blocks for reference ----------------------------
  6. Kamal
    Kamal avatar
    34 posts
    Member since:
    Jan 2013

    Posted 03 Mar 2015 in reply to Kamal Link to this post

    Hello Shinu

    Please nevermind.
    Setting EnableLinqExpressions="false" solved my issue. Though I'm not sure how and what are it's internal semantics.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017