Telerik Grid Filter不在Date列上工作

时间:2012-02-16 03:21:32

标签: grid telerik

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

这是我的代码:

Html.Telerik()。网格()

        .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"))
%>

2 个答案:

答案 0 :(得分:0)

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

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

  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

然后从像

这样的网格的need数据源事件中调用它
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

享受:)

答案 1 :(得分:0)

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;
                }
            }
        }
    }