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