Telerik 网格过滤器不适用于日期列

我有一个 Telerik Grid,其中一列具有日期时间值,但该字段上的过滤器不起作用,因为默认情况下过滤器只接受日期。有什么方法可以将时间包含在 Telerik 网格过滤器中?

这是我的代码:

Html.Telerik().Grid()

        .Name("FilesGrid")
        .Sortable(sorting => sorting.OrderBy(sortOrder => sortOrder.Add(o => o.COL1)))
        .Pageable(paging =>
            paging.PageSize(10)
                    .Style(GridPagerStyles.NextPreviousAndNumeric)
                    .Position(GridPagerPosition.Bottom)
        ).Filterable()
        .Groupable()
        .Selectable()
        .Resizable(c => c.Columns(true))
        .DataBinding(dataBinding => dataBinding.
            Ajax()
                .Select("_Grid", "Files")
                .Update("_ClientUpdate", "Todos")
                .Insert("_ClientInsert", "Todos")
                .Delete("Delete", "Files"))
                        .EnableCustomBinding(true)  
        .Columns(columns =>
        {
            columns.Bound(c => c.col1);
            columns.Bound(c => c.col2);
            columns.Bound(c => c.col3);
            columns.Bound(c => c.CreatedAt);  -------- this column is datetime but filter only has date calendar , hence the filter wont work
        .ClientEvents(events => events.OnDataBound("onGridDataBound"))
%>
stack overflow Telerik Grid Filter not working on Date column
原文答案

答案:

作者头像

我知道这是一篇旧帖子,但我会为那些搜索它的人提供解决方案

我已经定制了这个解决方案,它适用于所有场景,如果你有多个列,你也可以使用它有一个日期时间过滤器在需要数据源事件中使用这个函数首先把这个函数放在你的代码中

  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.adddays(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.adddays(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.adddays(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.adddays(1).tostring() + "') ")

        End Select

然后你像这样从网格的需要数据源事件中调用它

Dim filter As String = grdReport.MasterTableView.FilterExpression
            If (filter.contains("LAST_UPD_DATE")) Then
                manageDateFilter("LAST_UPD_DATE", source, filter, 6)
            End If

对于多日期列,您可以像这样使用它

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

请享用 :)

作者头像
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;
                }
            }
        }
    }