This question is locked. New answers and comments are not allowed.
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:
View:
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
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