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

[Solved] Grid FilterExpression problem

2 Answers 150 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nikola
Top achievements
Rank 2
Nikola asked on 16 May 2013, 08:29 AM

I'm trying to filter two different (Data Picker's) that are in the same column at the grid. I try to reach a command with these two fields. The whole filtering process is going on in the code behind, and so far it is working for the other fields in the grid. But for one I'm getting a strange exception:

{"The argument types 'Edm.DateTime' and 'Edm.String' are incompatible for this operation. Near greater than or equals expression, line 6, column 15."}

In the DB the field is a datetime and it is a single column

My code :

                    <telerik:GridBoundColumn DataField="EntryDate" DataType="System.DateTime" DataFormatString="{0:dd/MM/yyyy}" FilterControlAltText="Filter EntryDate column"
                        HeaderText="Date" SortExpression="EntryDate" UniqueName="EntryDate" FilterControlWidth="50px">
                        <FilterTemplate>
                            From
                            <telerik:RadDatePicker ID="FromOrderDatePicker" runat="server" Width="100px" AutoPostBack="true"
                                OnSelectedDateChanged="FromOrderDatePicker_SelectedDateChanged" OnPreRender="FromOrderDatePicker_PreRender" />
                            to
                            <telerik:RadDatePicker ID="ToOrderDatePicker" runat="server" Width="100px" AutoPostBack="true"
                                OnSelectedDateChanged="ToOrderDatePicker_SelectedDateChanged" OnPreRender="ToOrderDatePicker_PreRender" />

                        </FilterTemplate>
                    </telerik:GridBoundColumn> 

Code behind:

     protected void FromOrderDatePicker_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
            {
                RadDatePicker picker = sender as RadDatePicker;
                ViewState["FromDatePicker"] = DateTimeToString(picker.SelectedDate.Value);

                ExpressionChanged();
            }
            protected void FromOrderDatePicker_PreRender(object sender, EventArgs e)
            {
                if (ViewState["FromDatePicker"] != null)
                {
                    RadDatePicker picker = sender as RadDatePicker;
                    picker.SelectedDate = StringToDateTime(ViewState["FromDatePicker"].ToString());
                }
            }


  private string DateTimeToString(DateTime dt)
    {
        return dt.Month + "/" + dt.Day + "/" + dt.Year;
    }
    private DateTime StringToDateTime(string s)
    {
        string[] dts = s.Split('/');
        DateTime dt = new DateTime(Int32.Parse(dts[2]), Int32.Parse(dts[0]), Int32.Parse(dts[1]));
        return dt;
    }

It would be just redundant to post the code (for the toDatePicker)

 private void ExpressionChanged()
    {
        //reset
        TimeReportGrid.MasterTableView.FilterExpression = null;
        TimeReportGrid.MasterTableView.Rebind();


        if (ViewState["FromDatePicker"] != null &&
            ViewState["ToDatePicker"] != null &&
            ViewState["FromDatePicker"].ToString() != "" &&
            ViewState["ToDatePicker"].ToString() != "")
        {
            TimeReportGrid.MasterTableView.FilterExpression += "(it.EntryDate >= '" + ViewState["FromDatePicker"].ToString() + "') AND (it.EntryDate <= '" + ViewState["ToDatePicker"].ToString() + "')";
        }

feel free to ask if there is any problem

Thanks for help and fast answer !

2 Answers, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 20 May 2013, 02:50 PM
Hi,

 The code looks properly configured. The error indicates a type mismatch, meaning that somewhere the application is trying to compare string to DateTime type. In case you are using Entity Framework you can check whether the auto-generated fields in the classes have the correct type as in the database.
Further information on working with filter expressions can be found here;
http://www.telerik.com/help/aspnet-ajax/grid-operate-with-filter-expression-manually.html
as well as a demo with a column filtering with two DateTime pickers is available here:
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/filtertemplate/defaultcs.aspx

Additionally the grid also supports built-in range filtering for the GridDateTimeColumn in which case the control will automatically render two RadDatePicker controls in the filter template and will filter the results accordingly without the need to write custom server-side code.

Kind regards,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Nikola
Top achievements
Rank 2
answered on 21 May 2013, 06:48 AM
I solve the problem at the end it was pretty simple I had just to add (Datatype) in my case a Date Time and that solve my problem 

TimeReportGrid.MasterTableView.FilterExpression += "(it.EntryDate>= DATETIME '" + ViewState["FromDatePicker"].ToString() + " 00:00') AND (it.EntryDate<= DATETIME '" + ViewState["ToDatePicker"].ToString() + " 23:59')";
Tags
Grid
Asked by
Nikola
Top achievements
Rank 2
Answers by
Marin
Telerik team
Nikola
Top achievements
Rank 2
Share this question
or