Filtering by date

2 posts, 0 answers
  1. Paul Taylor
    Paul Taylor avatar
    41 posts
    Member since:
    Jun 2010

    Posted 05 Apr 2011 Link to this post

    I have a grid with filtering enabled, bound to a DataTable object so that it supports automatic sorting.

    There is a date field in the table, and as the RadGrid needs the date converting to mm/dd/yyyy format before it will filter correctly, I have intercepted the Filter command, cancelled it, and have rebound the list with the FilterExpression correctly set. See code below.

            protected void rgResults_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
            {
                try
                {
                    if (e.Item is GridFilteringItem)
                    {
                        Pair filterPair = (Pair)e.CommandArgument;
                        if (e.CommandName == RadGrid.FilterCommandName && filterPair.Second.ToString() == "Date")
                        {
                            e.Canceled = true;
                            GridFilteringItem item = (GridFilteringItem)e.Item;
                            GridBoundColumn col = (GridBoundColumn)item.OwnerTableView.GetColumnSafe(filterPair.Second.ToString());
                            DateTime date = DateTime.Parse(col.CurrentFilterValue);
                            item.OwnerTableView.FilterExpression = string.Format("([{0}] {1} #{2}#)", filterPair.Second.ToString(),       
                                                                          GetOperator(filterPair.First.ToString()), date.ToString("d", new CultureInfo("en-US")));
                            item.OwnerTableView.Rebind();
                        }
                    }
    ...

    To create the filter expression in T-SQL syntax, I need to get the T-SQL equivalent of the RadGrid filter operator. Is there a function I can use to do this? The grid obviously has to do the same to construct the filter expression, so I guess the functionality is in there somewher. Is it available through the API?

    Thanks in advance,
  2. Paul Taylor
    Paul Taylor avatar
    41 posts
    Member since:
    Jun 2010

    Posted 05 Apr 2011 Link to this post

    Found the answer: the documentation that I was following is out of date. No special action is needed to filter by date. Just removed the code and it worked!
Back to Top