I have a Telerik Grid with one of the column having Date Time values, but the filter on that field is not working as the filter only accepts date by default. Is there any way we
protected void ItemsRadGrid_ItemCommand(object source, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.FilterCommandName)
{
try
{
FillItemsGrid();
FilterByDate(ItemsRadGrid, e);
}
catch (Exception ex)
{
//ExceptionLogClass.InsertError(ex);
}
}
}
public static void FilterByDate(RadGrid grid, GridCommandEventArgs e)
{
Pair filterPair = e.CommandArgument as Pair;
string columnName = Convert.ToString(filterPair.Second);
if (filterPair.First.ToString() == "NoFilter")
{ }
else
{
if (grid.Columns.FindByDataField(columnName).DataType.Name == "DateTime")
{
try
{
TextBox FilterColumnField = ((TextBox)((GridFilteringItem)e.Item)[columnName].Controls[0]);
string oldDate = FilterColumnField.Text;
FilterColumnField.Text = DateTime.ParseExact(FilterColumnField.Text, "dd.MM.yyyy", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy 12:00:00 tt");
}
catch (FormatException)
{
e.Canceled = true;
}
}
}
}
I know it's an old post but I will put a solution for those people search for it
I have customized this solution, it works with all scenarios and you can use it also if you have more than one column have a date time filter use this function in the need data source event first put this function in your code
Protected Sub manageDateFilter(ByVal columnName As String, ByVal source As Object, ByVal filter As String, ByVal columnindex As Integer)
Select Case DirectCast(DirectCast(source, Telerik.Web.UI.RadGrid).Columns.Item(columnindex), Telerik.Web.UI.GridDateTimeColumn).CurrentFilterFunction
Case GridKnownFunction.EqualTo
Dim date1 As datetime = convert.todatetime(DirectCast(DirectCast(source, Telerik.Web.UI.RadGrid).Columns.Item(columnindex), Telerik.Web.UI.GridDateTimeColumn).CurrentFilterValue)
grdReport.MasterTableView.FilterExpression = filter.replace("([" + columnName + "] = " + filter.substring(filter.indexof(columnName) + (columnName.length + 4)).trim().substring(0, filter.substring(filter.indexof(columnName) + (columnName.length + 4)).indexof("M") + 2) + ")".trim(), "([" + columnName + "] >= '" + date1.tostring() + "') AND ([" + columnName + "] < '" + date1.affffdays(1).tostring() + "')")
Case GridKnownFunction.NotEqualTo
Dim date1 As datetime = convert.todatetime(DirectCast(DirectCast(source, Telerik.Web.UI.RadGrid).Columns.Item(columnindex), Telerik.Web.UI.GridDateTimeColumn).CurrentFilterValue)
grdReport.MasterTableView.FilterExpression = filter.replace("([" + columnName + "] <> " + filter.substring(filter.indexof(columnName) + (columnName.length + 4)).trim().substring(0, filter.substring(filter.indexof(columnName) + (columnName.length + 5)).indexof("M") + 2) + ")".trim(), "(([" + columnName + "] < '" + date1.tostring() + "') OR ([" + columnName + "] >= '" + date1.affffdays(1).tostring() + "'))")
Case GridKnownFunction.GreaterThanOrEqualTo
Dim date1 As datetime = convert.todatetime(DirectCast(DirectCast(source, Telerik.Web.UI.RadGrid).Columns.Item(columnindex), Telerik.Web.UI.GridDateTimeColumn).CurrentFilterValue)
grdReport.MasterTableView.FilterExpression = filter.replace("([" + columnName + "] >= " + filter.substring(filter.indexof(columnName) + (columnName.length + 4)).trim().substring(0, filter.substring(filter.indexof(columnName) + (columnName.length + 4)).indexof("M") + 2) + ")".trim(), "([" + columnName + "] >= '" + date1.tostring() + "')")
Case GridKnownFunction.LessThanOrEqualTo
Dim date1 As datetime = convert.todatetime(DirectCast(DirectCast(source, Telerik.Web.UI.RadGrid).Columns.Item(columnindex), Telerik.Web.UI.GridDateTimeColumn).CurrentFilterValue)
grdReport.MasterTableView.FilterExpression = filter.replace("([" + columnName + "] <= " + filter.substring(filter.indexof(columnName) + (columnName.length + 4)).trim().substring(0, filter.substring(filter.indexof(columnName) + (columnName.length + 5)).indexof("M") + 2) + ")".trim(), "([" + columnName + "] <= '" + date1.affffdays(1).tostring() + "') ")
Case GridKnownFunction.GreaterThan
Dim date1 As datetime = convert.todatetime(DirectCast(DirectCast(source, Telerik.Web.UI.RadGrid).Columns.Item(columnindex), Telerik.Web.UI.GridDateTimeColumn).CurrentFilterValue)
grdReport.MasterTableView.FilterExpression = filter.replace("([" + columnName + "] > " + filter.substring(filter.indexof(columnName) + (columnName.length + 4)).trim().substring(0, filter.substring(filter.indexof(columnName) + (columnName.length + 4)).indexof("M") + 2) + ")".trim(), "([" + columnName + "] >= '" + date1.affffdays(1).tostring() + "') ")
End Select
and then you call it from the need data source event for the grid like this
Dim filter As String = grdReport.MasterTableView.FilterExpression
If (filter.contains("LAST_UPD_DATE")) Then
manageDateFilter("LAST_UPD_DATE", source, filter, 6)
End If
and for multi date column you can use it like this
Dim filter As String = ReportGrid.MasterTableView.FilterExpression
If (filter.contains("START_DATE")) Then
manageDateFilter("START_DATE", source, filter, 5)
End If
filter = ReportGrid.MasterTableView.FilterExpression
If (filter.contains("END_DATE")) Then
manageDateFilter("END_DATE", source, filter, 6)
End If
enjoy :)