Date filtering in RadGrids other than MM/dd/yyyy format

4 posts, 0 answers
  1. Debashis Pyne
    Debashis Pyne avatar
    66 posts
    Member since:
    Apr 2010

    Posted 26 Oct 2010 Link to this post

     

    Hi,

     

    I am having a radgrid with a Date field in dd/MM/yyyy format as per your site’s codes. I am having problem with filtering with Date-field in the grid, where I am getting an error on “filterItem.OwnerTableView.Rebind(); , saying “Telerik.Web.UI.ParseException: Expression expected”.  My code is as follows

     

    Any idea , where I am wrong?

     

     

     

    ===================================

     

     

    <telerik:RadGrid ID="grdActionList" Skin="WebBlue"

                                    runat="server" GridLines="None"

                                    AutoGenerateColumns="False"

                                    AllowSorting="True"

                                    AllowFilteringByColumn = "true"

                                    AllowPaging="True" PageSize="10"

                                    OnNeedDataSource="grdActionList_NeedDataSource"

                                    OnItemCommand="grdActionList_ItemCommand">

                                   

                                    <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>                               

                                    <MasterTableView DataKeyNames="Action_Id" CommandItemDisplay="Top" EditMode="PopUp">

                                    <CommandItemSettings ShowAddNewRecordButton="false"/>

                                    <EditFormSettings PopUpSettings-Modal="true" />

                                    <Columns>

     

                                        <telerik:GridBoundColumn DataField="Action_Id" DataType="System.Int32"

                                            HeaderText="Action_Id" ReadOnly="True" SortExpression="Action_Id"

                                            UniqueName="Action_Id" Visible="false" >

                                        </telerik:GridBoundColumn>

                                       

                                        <telerik:GridBoundColumn DataField="Status" DataType="System.Int32" Visible="false"

                                            UniqueName="Status" ReadOnly="true">

                                        </telerik:GridBoundColumn>

                                        

                                        <telerik:GridBoundColumn DataField="Content_Id" DataType="System.Int32"

                                            HeaderText="Content_Id" SortExpression="Content_Id"

                                            UniqueName="Content_Id" ReadOnly="true" Visible="false">

                                        </telerik:GridBoundColumn>

                                       

                                       

                                         <telerik:GridBoundColumn DataField="Create_Date" DataFormatString="{0:dd/MM/yyyy}"

                                            HeaderText="Created On" SortExpression="Create_Date" ItemStyle-Width="150"

                                            UniqueName="Create_Date" ReadOnly="true">

                                        </telerik:GridBoundColumn>

                                       

                                        <telerik:GridBoundColumn DataField="JOMC_ID"

                                            HeaderText="Unique Id" SortExpression="JOMC_ID" ItemStyle-Width="150"

                                            UniqueName="JOMC_ID" ReadOnly="true">

                                        </telerik:GridBoundColumn>

                                       

                                        <telerik:GridBoundColumn DataField="Module_Content_Text"

                                            HeaderText="Action name" SortExpression="Module_Content_Text"

                                            UniqueName="Module_Content_Text" ItemStyle-Width="450">

                                        </telerik:GridBoundColumn>

                                       

                                      

                                    </Columns>

                                    </MasterTableView>

                                    <ClientSettings EnableRowHoverStyle="true"></ClientSettings>

                                    </telerik:RadGrid>

     

    protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                displayActionGrid();

            }

     

        }

     

     

     

     

        protected void grdActionList_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)

        {

            #region --------------Filtering-------------------------------

            if ((e.CommandName == RadGrid.FilterCommandName ) && (((Pair)e.CommandArgument).Second.ToString() == "Create_Date") &&

                (((Pair)e.CommandArgument).First.ToString() != "NoFilter"))

                          

            {

              e.Canceled = true;

              GridFilteringItem filterItem = (GridFilteringItem)e.Item;

              //string currentPattern = (TextBox)filterItem(((Pair)e.CommandArgument).Second).Controls(0).Text;

              //string currentPattern = "{0:dd/MM/yyyy}";

              string currentPattern = "dd/MM/yyyy";

              string filterPattern = "";

              string filterPatternAssist= "";

              //char sep = "a";

              if (currentPattern.IndexOf(" ") != -1)

              {

                currentPattern = currentPattern.Replace(" ", "/");

              }

              string[] vals = currentPattern.Split('/');

              string filterOption = Convert.ToString(((Pair)e.CommandArgument).First);

     

              if (filterOption != "IsNull" && filterOption != "NotIsNull")

              {

                if (vals.Length > 3)

                {

                    filterPatternAssist = vals[4] + "/" + vals[3] + "/" + vals[5];

                }

                filterPattern = vals[1] + "/" + vals[0] + "/" + vals[2];

              }

     

              GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe( "Create_Date");

     

              switch(filterOption)

              {

                            case "EqualTo":

                               filterPattern = "[Create_Date] = '" + filterPattern + "'";

                               dateColumn.CurrentFilterFunction = GridKnownFunction.EqualTo;

                               break;

                            case "NotEqualTo":

                               filterPattern = "Not [Create_Date] = '" + filterPattern + "'";

                               dateColumn.CurrentFilterFunction = GridKnownFunction.NotEqualTo;

                               break;

                            case "GreaterThan":

                               filterPattern = "[Create_Date] > '" + filterPattern + "'";

                               dateColumn.CurrentFilterFunction = GridKnownFunction.GreaterThan;

                               break;

                            case "LessThan":

                               filterPattern = "[Create_Date] < '" + filterPattern + "'";

                               dateColumn.CurrentFilterFunction = GridKnownFunction.LessThan;

                               break;

                            case "GreaterThanOrEqualTo":

                               filterPattern = "[Create_Date] >= '" + filterPattern + "'";

                               dateColumn.CurrentFilterFunction = GridKnownFunction.GreaterThanOrEqualTo;

                               break;

                            case "LessThanOrEqualTo":

                               filterPattern = "[Create_Date] <= '" + filterPattern + "'";

                               dateColumn.CurrentFilterFunction = GridKnownFunction.LessThanOrEqualTo;

                               break;

                            case "Between":

                               filterPattern = "'" + filterPattern + "' <= [Create_Date] AND [Create_Date] <= '" + filterPatternAssist + "'";

                               dateColumn.CurrentFilterFunction = GridKnownFunction.Between;

                               break;

                            case "NotBetween":

                               filterPattern = "[Create_Date] <= '" + filterPattern + "' OR [Create_Date] >= '" + filterPatternAssist + "'";

                               dateColumn.CurrentFilterFunction = GridKnownFunction.NotBetween;

                               break;

                            case "IsNull":

                               break;

                            case "NotIsNull":

                               break;

               }

              foreach (GridColumn column in grdActionList.MasterTableView.Columns)

              {

                if (column.UniqueName != "Create_Date")

                {

                  column.CurrentFilterFunction = GridKnownFunction.NoFilter;

                  column.CurrentFilterValue = string.Empty;

                }

              }

              Session["filterPattern"] = filterPattern;

     

              dateColumn.CurrentFilterValue = currentPattern;

              filterItem.OwnerTableView.Rebind();

                

            }

            //Add more conditional checks for commands here if necessary

            else if (e.CommandName != RadGrid.SortCommandName && e.CommandName != RadGrid.PageCommandName)

            {

              Session["filterPattern"] = null;

              GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe( "Create_Date");

              dateColumn.CurrentFilterFunction = GridKnownFunction.NoFilter;

              dateColumn.CurrentFilterValue = string.Empty;

            }

     

            #endregion

     

     

        }

     

     

        #region Radgrid integration

        private void displayActionGrid()

        {

            JOMCMiddleWare.BL.Actions oAction = new JOMCMiddleWare.BL.Actions();

            DataTable dt = new DataTable();

            oAction._Company_Id = System.Convert.ToInt32(Session[JOMCSession.__Session_Company_Id]);

            oAction._Language_Id = System.Convert.ToInt32(Session[JOMCSession.__Session_Lanuage_Id]);

            dt = oAction.GetDataAllAction();

            grdActionList.DataSource = dt;

            grdActionList.DataBind();

        }

     

        private DataTable GridSource

        {

            get

            {

                JOMCMiddleWare.BL.Actions oAction = new JOMCMiddleWare.BL.Actions();

                DataTable dt = new DataTable();

                oAction._Company_Id = System.Convert.ToInt32(Session[JOMCSession.__Session_Company_Id]);

                oAction._Language_Id = System.Convert.ToInt32(Session[JOMCSession.__Session_Lanuage_Id]);

                dt = oAction.GetDataAllAction();

                return dt;

            }

        }

     

        protected void grdActionList_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)

        {

            grdActionList.DataSource = this.GridSource;

            //----------MS------26-10-------------

            if (Session["filterPattern"] != null)

            {

                grdActionList.MasterTableView.FilterExpression = (string)Session["filterPattern"];

            }

            //----------ME------------------

        }

     

     

        #endregion

    Thanks!!

     

  2. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 29 Oct 2010 Link to this post

    Hello Debashis,

    I noticed that you have opened a duplicate post on the same matter. Please, refer to the support ticket for additional information. To avoid duplicate posts, I suggest you continue the communication there.
    Additionally you could try setting EnableLinqExpressioins to false for the grid, this should prevent the parsing exception.

    Looking forward for your reply.

    Kind regards,
    Radoslav
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Maharnab
    Maharnab avatar
    1 posts
    Member since:
    Oct 2010

    Posted 29 Oct 2010 Link to this post

    Hi,

    I am also having same problem. As per your article(http://www.telerik.com/help/aspnet-ajax/grdfilteringfordatetimecolumnwithdataformatstring.html) , I have created the grid-view with mock Datatable(please find the content as follows), Please go through it and suggest a way out.

    Thanks
    <%@ Page Title="" Language="C#" CodeFile="ActionTest.aspx.cs" Inherits="Action" %>

    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
        <html>
    <body>
       
    <form id="test" runat="server">
         <%-- <asp:ScriptManager ID="ScriptManager" runat="server" />--%>
           <telerik:RadScriptManager runat="server" ID="RadScriptManager1">
        </telerik:RadScriptManager>

    <script language="javascript"> </script>
      <div>  
        <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
                <script type="text/javascript">

                </script>
            </telerik:RadCodeBlock>
       <div>

       <!--/******************************************************action button area start************************************************************/-->
    <div style="clear:both;" >

                                                                    
                                    <telerik:RadGrid ID="grdActionList" Skin="WebBlue"
                                    runat="server" GridLines="None"
                                    AutoGenerateColumns="False"
                                    AllowSorting="True"
                                    AllowFilteringByColumn = "true"
                                    AllowPaging="True" PageSize="10"
                                    EnableLinqExpressioins="false"
                                    OnNeedDataSource="grdActionList_NeedDataSource"
                                    OnItemCommand="grdActionList_ItemCommand">
                                    
                                    <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>                                
                                    <MasterTableView DataKeyNames="Action_Id" CommandItemDisplay="Top" EditMode="PopUp">
                                    <CommandItemSettings ShowAddNewRecordButton="false"/>
                                    <EditFormSettings PopUpSettings-Modal="true" />
                                    <Columns>

                                        <telerik:GridBoundColumn DataField="Action_Id" DataType="System.Int32"
                                            HeaderText="Action_Id" ReadOnly="True" SortExpression="Action_Id"
                                            UniqueName="Action_Id" Visible="false" >
                                        </telerik:GridBoundColumn>

                                        
                                         <telerik:GridBoundColumn DataField="Create_Date" DataFormatString="{0:dd/MM/yyyy}"
                                            HeaderText="Created On" SortExpression="Create_Date" ItemStyle-Width="150"
                                            UniqueName="Create_Date" ReadOnly="true">
                                        </telerik:GridBoundColumn>
                                        
                                        <telerik:GridBoundColumn DataField="Module_Content_Text"
                                            HeaderText="Action name" SortExpression="Module_Content_Text"
                                            UniqueName="Module_Content_Text" ItemStyle-Width="450">
                                        </telerik:GridBoundColumn>
                                        
                                       
                                    </Columns>
                                    </MasterTableView>
                                    <ClientSettings EnableRowHoverStyle="true"></ClientSettings>
                                    </telerik:RadGrid>
                                    </asp:Panel>
                                    
                                    <telerik:RadWindowManager ID="RadWindowManager1" runat="server" Behaviors="Close, Move, Reload">
                                        <Windows>
                                            <telerik:RadWindow ID="UserListDialog" runat="server" Title="Manage actions" Height="610px"
                                                Width="575px" Left="150px" ReloadOnShow="true" ShowContentDuringLoad="false" Modal="true"
                                                VisibleStatusbar="false" Animation="Fade" />
                                        </Windows>
                                    </telerik:RadWindowManager>

                                    
                                   <%-- *************** GRID DISPLAY ENDS ***************--%>
      </div>
     
     
     
      <!--/******************************************************grid display  end************************************************************/-->
       </div>
    </div>
    <%--</asp:Content>--%>
    </form>
    </body>
    </html>
    -----------------------------------------------------------------------------------------
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using Telerik.Web.UI;

    public partial class Action : System.Web.UI.Page
    {
           
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                     displayActionGrid();
                }
            }
        protected void grdActionList_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            #region --------------Filtering-------------------------------
            if ((e.CommandName == RadGrid.FilterCommandName ) && (((Pair)e.CommandArgument).Second.ToString() == "Create_Date") &&
                (((Pair)e.CommandArgument).First.ToString() != "NoFilter") )
                           
            {
              e.Canceled = true;
              GridFilteringItem filterItem = (GridFilteringItem)e.Item;
              TextBox filterBox = (e.Item as GridFilteringItem)[((Pair)e.CommandArgument).Second.ToString()].Controls[0] as TextBox;
              string currentPattern = filterBox.Text;   
              string filterPattern = "";
              string filterPatternAssist= "";

              string filtVal = "";//--Maha-Implemented-----

              if (currentPattern.IndexOf(" ") != -1)
              {
                currentPattern = currentPattern.Replace(" ", "/");
              }
              string[] vals = currentPattern.Split('/');
              string filterOption = Convert.ToString(((Pair)e.CommandArgument).First);

              if (filterOption != "IsNull" && filterOption != "NotIsNull")
              {
                if (vals.Length > 3)
                {
                    filterPatternAssist = vals[4] + "/" + vals[3] + "/" + vals[5];
                }
                filterPattern = vals[1] + "/" + vals[0] + "/" + vals[2];
                filtVal = filterPattern;
              }

              GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe("Create_Date");
             // GridColumn dateColumn = grdActionList.MasterTableView.GetColumnSafe("Create_Date");

              switch(filterOption)
              {
                            case "EqualTo":
                               filterPattern = "[Create_Date] = '" + filterPattern + "'";
                               dateColumn.CurrentFilterFunction = GridKnownFunction.EqualTo;
                               break;
                            case "NotEqualTo":
                               filterPattern = "Not [Create_Date] = '" + filterPattern + "'";
                               dateColumn.CurrentFilterFunction = GridKnownFunction.NotEqualTo;
                               break;
                            case "GreaterThan":
                               filterPattern = "[Create_Date] > '" + filterPattern + "'";
                               dateColumn.CurrentFilterFunction = GridKnownFunction.GreaterThan;
                               break;
                            case "LessThan":
                               filterPattern = "[Create_Date] < '" + filterPattern + "'";
                               dateColumn.CurrentFilterFunction = GridKnownFunction.LessThan;
                               break;
                            case "GreaterThanOrEqualTo":
                               filterPattern = "[Create_Date] >= '" + filterPattern + "'";
                               dateColumn.CurrentFilterFunction = GridKnownFunction.GreaterThanOrEqualTo;
                               break;
                            case "LessThanOrEqualTo":
                               filterPattern = "[Create_Date] <= '" + filterPattern + "'";
                               dateColumn.CurrentFilterFunction = GridKnownFunction.LessThanOrEqualTo;
                               break;
                            case "Between":
                               filterPattern = "'" + filterPattern + "' <= [Create_Date] AND [Create_Date] <= '" + filterPatternAssist + "'";
                               dateColumn.CurrentFilterFunction = GridKnownFunction.Between;
                               break;
                            case "NotBetween":
                               filterPattern = "[Create_Date] <= '" + filterPattern + "' OR [Create_Date] >= '" + filterPatternAssist + "'";
                               dateColumn.CurrentFilterFunction = GridKnownFunction.NotBetween;
                               break;
                            case "IsNull":
                               break;
                            case "NotIsNull":
                               break;
               }
              foreach (GridColumn column in grdActionList.MasterTableView.Columns)
              {
                if (column.UniqueName != "Create_Date")
                {
                  column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                  column.CurrentFilterValue = string.Empty;
                }
              }
              Session["filterPattern"] = filterPattern;
              dateColumn.CurrentFilterValue = filtVal; //----????----
              filterItem.OwnerTableView.Rebind();
            }
            //Add more conditional checks for commands here if necessary
            else if (e.CommandName != RadGrid.SortCommandName && e.CommandName != RadGrid.PageCommandName)
            {
              Session["filterPattern"] = null;
              GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe("Create_Date");
              dateColumn.CurrentFilterFunction = GridKnownFunction.NoFilter;
              dateColumn.CurrentFilterValue = string.Empty;
            }
            #endregion
        }


        #region Radgrid integration
        private void displayActionGrid()
        {
            //JOMCMiddleWare.BL.Actions oAction = new JOMCMiddleWare.BL.Actions();
            DataTable dt2 = new DataTable();
           // oAction._Company_Id = System.Convert.ToInt32(Session[JOMCSession.__Session_Company_Id]);
           // oAction._Language_Id = System.Convert.ToInt32(Session[JOMCSession.__Session_Lanuage_Id]);
           // dt = oAction.GetDataAllAction();
            dt2 = GetDataMockTable();
            grdActionList.DataSource = dt2;
            grdActionList.DataBind();
        }

        private DataTable GridSource
        {
            get
            {
               // JOMCMiddleWare.BL.Actions oAction = new JOMCMiddleWare.BL.Actions();
                DataTable dt1 = new DataTable();
              //  oAction._Company_Id = System.Convert.ToInt32(Session[JOMCSession.__Session_Company_Id]);
              //  oAction._Language_Id = System.Convert.ToInt32(Session[JOMCSession.__Session_Lanuage_Id]);
               // dt = oAction.GetDataAllAction();
                dt1 = GetDataMockTable();
                return dt1;
            }
        }

        protected void grdActionList_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            grdActionList.DataSource = this.GridSource;
            //----------MS------26-10-------------
            if (Session["filterPattern"] != null)
            {
                string a = (string)Session["filterPattern"];
                grdActionList.MasterTableView.FilterExpression = (string)Session["filterPattern"];
            }
            //----------ME------------------
        }
     
        #endregion

        //--------------------------------------------------------
        //creating mock table for data
        protected DataTable GetDataMockTable()
        {
            DataTable dtable = new DataTable();
            DataColumn column;

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "Action_Id";
            dtable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.DateTime");
            column.ColumnName = "Create_Date";
            dtable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Module_Content_Text";
            dtable.Columns.Add(column);

            dtable.Rows.Add(new object[] { "1", "2010-05-14", "AA" });
            dtable.Rows.Add(new object[] { "2", "2009-07-01", "BB" });
            dtable.Rows.Add(new object[] { "3", "2010-08-24", "CC" });
            dtable.Rows.Add(new object[] { "4", "2010-09-28", "DD" });

            return dtable;
        }


       


        
    }

  5. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 03 Nov 2010 Link to this post

    Hello Maharnab,

    I apologize for misleading you, the right name of the RadGrid property is EnableLinqExpressions, instead of EnableLinqExpressioins. When you set the property with the wrong name the RadGrid just skipped it and the RadGrid LinqExpressions are still enabled. Setting the EnableLinqExpressions="false" will disable the LinqExpressions and the filtering will work properly. Also I am sending you a simple example based on your code.

    All the best,
    Radoslav
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top