This is a migrated thread and some comments may be shown as answers.

RadGrid Date Filtering

1 Answer 238 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carl
Top achievements
Rank 1
Carl asked on 04 Apr 2012, 03:25 PM
Hi

I am evaluating the RadGrid control for an upcoming project and have been struggling with a particular scenario.

I need to filter a date column to include all rows in a certain range. I've worked out how to do that okay with the FilterExpression property as shown below:
 

 

 protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.FilterCommandName)
            {
                Pair filterPair = (Pair)e.CommandArgument;
         
                if (filterPair.First.ToString() == "EqualTo" && filterPair.Second.ToString() == "OrderDate")
                {
                    e.Canceled = true;
                    RadDatePicker filterBox = (e.Item as GridFilteringItem)[filterPair.Second.ToString()].Controls[0] as RadDatePicker;

                    string newFilter = String.Format("((it.[{0}] >= DATETIME'{1}') AND (it.[{0}] <= DATETIME'{2}'))", filterPair.Second, filterBox.SelectedDate.Value.ToString("yyyy-MM-dd" + " 00:00:00"), filterBox.SelectedDate.Value.ToString("yyyy-MM-dd") + " 23:59:59");
                  
                 
                    if (RadGrid1.MasterTableView.FilterExpression == "")
                    {
                        RadGrid1.MasterTableView.FilterExpression = newFilter;
                    }
                    else
                    {
                        RadGrid1.MasterTableView.FilterExpression = String.Format("(({0}) AND ({1}))", RadGrid1.MasterTableView.FilterExpression, newFilter);
                    }
                   
                    GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe("OrderDate");
                
                    dateColumn.CurrentFilterValue = filterBox.SelectedDate.Value.ToString();
                    dateColumn.CurrentFilterFunction = GridKnownFunction.EqualTo;

                }

                RadGrid1.Rebind();
          
            }
          
        }

 

 

 

 

However, the problem I have is this.

When I filter one columns date this works okay, however, if I filter another column the date filtered column is undone (it refers to trying to filter by the entries set in dateColumn.CurrentFilterValue .

Also, an additional problem I have with this code is, when I filter the date column twice, both filters are appended together. So if I filter by 03/04/2012 the first result set is correct, if I then filter by 04/04/2012 the second filterexpression is appended to the first (in essence where date = 03/04/012 AND date = 04/04/2012).

Could someone point me in the right direction as to how to filter multiple columns, one of which is filtered using a manual filter expression and also to take into account multiple attempts at the same filter. Should I be maintaining the filters in session or is there a cleaner way to achieve a solution?

Many thanks
Carl

1 Answer, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 09 Apr 2012, 11:25 AM
Hi Carl,

You would need to manually append and remove the custom expression, as RadGrid is not able to work with the custom parts of the FilterExpression that you add. I am attaching a project demonstrating a similar situation. The last column in the grid has a custom filter, where you can list the filter values separated with commas and the filtering will return all records that contain the listed strings. At the same time, other columns filtering works without wiping out this filter.

Regards,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Carl
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Share this question
or