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

[Solved] Error in ToGridModel() with a filter on DateTime

0 Answers 33 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
B
Top achievements
Rank 1
B asked on 22 Apr 2012, 02:05 AM
I'm displaying the results of dynamic queries in a grid based on a DataTable. Filtering is enabled and I need to allow the user to export the filtered results to a .csv file. I'm able to re-apply the filters on everything except DateTime columns using:

datatable.AsEnumerable().AsQueryable().ToGridModel(gridstate);

If the filter is on a DateTime column (an 'is after' filter, for example), I get an Operation Exception "The binary operator GreaterThan is not defined for the types 'System.Object' and 'System.DateTime'." 

I created a very simple example to reproduce the problem:

Controller:
    public ActionResult TestFilter(string filterBy)
        {
            DataTable dt = GetTable();
            GridState gs = new GridState();
                gs.Filter = filterBy;
                gs.Page = 1;
                gs.Size = int.MaxValue;
 
// The next line causes error:
 //The binary operator GreaterThan is not defined for the types 'System.Object' and 'System.DateTime'.
 
            var gridModel = dt.AsEnumerable().AsQueryable().ToGridModel(gs);
 
            return View("Index",GetTable());
        }
 
        private DataTable GetTable()
        {
            DataTable dt = new DataTable(); 
            dt.Columns.Add("TestDate", Type.GetType("System.DateTime"));
            DataRow dr;
            for (int x = 1; x <= 10; x++)
            {
                dr = dt.NewRow();
                dr["TestDate"] = DateTime.Now.AddDays(x);
                dt.Rows.Add(dr);
            }
            return dt;
        }
    

View:
@model System.Data.DataTable
            
@{
    ViewBag.Title = "Home Page";
}
 
<h2>@ViewBag.Message</h2>
 
@(Html.Telerik().Grid(Model)
        .Name("MyGrid")
         .ToolBar(commands => commands
            .Custom()
                .HtmlAttributes(new { id = "TestFilter", onclick = "command_onClick(this)" })
                .Text("Test Filter")
                .Action("TestFilter", "Home", new { filterBy = "~" }))
 
        .Columns(columns =>
        {
            foreach (System.Data.DataColumn column in Model.Columns)
            {
                columns.Bound(column.DataType, column.ColumnName);
            }
        })
        .DataBinding(dataBinding => dataBinding.Server())
        .Sortable()
        .Filterable()
        .Scrollable()
        .KeyboardNavigation()
        .Resizable(resizing => resizing.Columns(true)))
 
 
 
<script type="text/javascript">
 
 
    function command_onClick(e) {
     
        var grid = $('#MyGrid').data('tGrid');
 
        var $cmd = $('#TestFilter');
 
        // Get its 'href' attribute - the URL where it would navigate to
        var href = $cmd.attr('href');
 
        // Update the 'filter' parameter with the grids' current filtering state
        href = href.replace(/filterBy=(.*)/, 'filterBy=' + (grid.filterBy || '~'));
         
        // Update the 'href' attribute
        $cmd.attr('href', href);
    }
 
</script>

The attached Visual Studio solution reproduces the problem for me. Filter on the TestDate column, the click the "Test Filter" button.

Any ideas what the problem might be, or workarounds?

Thanks! ...and thanks for a great product.

Beth

telerik.web.mvc version: 2012.1.214.340
visual studio 2010 c#
windows 7
Tags
Grid
Asked by
B
Top achievements
Rank 1
Share this question
or