RadDatePicker not reflects Min and MaxDate in Radgrid Datetime Filter.

5 posts, 0 answers
  1. Ramesh
    Ramesh avatar
    16 posts
    Member since:
    Apr 2015

    Posted 27 Nov 2018 Link to this post

    Hi Team,

    I am using a radgrid in my application for the past 3 years.which has a Datetime column and the Filter option is enabled. Currently I am able to filter the date range from 1900 to 2100 perfectly.

    Se we had a scenario to enter the date less than 1900 and greater than 2100, but the default functionality is not allowed to enter those dates.

    So I used MinDate and MaxDate properties of the raddatepicker and is working all areas except the Filter area.

    Filter area, the calendar pop up allows only the default date range only ( 1900 to 2100), So I am unable to filter the date range beyond the ranges.

     

    Is this a bug or I am doing something wrong ? My Rad grid is created dynamically.

    Appreciate you support/suggestion if any.

     

    Regards,

    Ramesh.

     

  2. Marin Bratanov
    Admin
    Marin Bratanov avatar
    4489 posts

    Posted 28 Nov 2018 Link to this post

    Hello Ramesh,

    The general approach would be to use the ItemCreated event, capture the filtering item creation and access the desired controls to set their settings, like shown following article: https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/how-to/Filtering/change-filter-items.

    Here's an example I made for you:

    protected void rg1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridFilteringItem)
        {
            GridFilteringItem filteringItem = e.Item as GridFilteringItem;
            RadDatePicker fromPicker = filteringItem["theDateColumn"].Controls[1] as RadDatePicker;
            RadDatePicker toPicker = filteringItem["theDateColumn"].Controls[4] as RadDatePicker;
            //of course add checks here. With disabled range filtering the control tree will be different too
            //so you may need to take that into account. The same goes for a different render mode
            SetMinMaxDateToPicker(fromPicker);
            SetMinMaxDateToPicker(toPicker);
        }
    }
     
    protected void SetMinMaxDateToPicker(RadDatePicker picker)
    {
        picker.MinDate = DateTime.Now.AddDays(-1);
        picker.MaxDate = DateTime.Now.AddDays(2);
        //or other properties
    }
    based on the following declaration:

    <telerik:RadGrid runat="server" ID="rg1" RenderMode="Lightweight" OnNeedDataSource="rg1_NeedDataSource"
        OnItemCreated="rg1_ItemCreated" AllowFilteringByColumn="true">
        <MasterTableView AutoGenerateColumns="false">
            <Columns>
                <telerik:GridDateTimeColumn UniqueName="theDateColumn" EnableRangeFiltering="true" DataField="theDate" HeaderText="the dates column"></telerik:GridDateTimeColumn>
                <telerik:GridBoundColumn HeaderText="orders count" DataField="OrderCount"></telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    and here is some dummy data to get it running:

    protected void rg1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        (sender as RadGrid).DataSource = GetDatesData();
    }
     
    public DataTable GetDatesData()
    {
        DataTable table = new DataTable();
     
        table.Columns.Add("theDate", typeof(DateTime));
        table.Columns.Add("OrderCount", typeof(int));
        table.Columns.Add("extraData", typeof(int));
     
        table.Rows.Add(DateTime.Now.AddDays(1), 100, 2);
        table.Rows.Add(DateTime.Now.AddDays(2), 150, 10);
        table.Rows.Add(DateTime.Now.AddDays(3), 120, 5);
        table.Rows.Add(DateTime.Now.AddDays(4), 300, 10);
     
        return table;
    }


    Regards,
    Marin Bratanov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. Ramesh
    Ramesh avatar
    16 posts
    Member since:
    Apr 2015

    Posted 29 Nov 2018 in reply to Marin Bratanov Link to this post

    Hi Marin Bratanov,

    Thanks for your suggestion and comments. But I dont know why it is not working in my side. The only difference is I am creating the grid dynamically. And  I am using the telerik dll verion "RadControls for ASP.NET AJAX Q1 2013"

    Placing my sample code for your referenc;

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;

    namespace TelSample
    {
        public partial class TelSample : System.Web.UI.Page
        {
            RadGrid RadGrid1;
            protected void Page_Load(object sender, EventArgs e)
            {

            }

            protected void Page_Init(object sender, EventArgs e)
            {
                RadGrid1 = new RadGrid();
                RadGrid1.AutoGenerateColumns = false;
                createColumn();
                RadGrid1.DataSource = createDataTable();
                RadGrid1.AllowFilteringByColumn = true;
                

                RadGrid1.NeedDataSource += RadGrid1_NeedDataSource;
                RadGrid1.PreRender += RadGrid1_PreRender;
                RadGrid1.ItemCreated += RadGrid1_ItemCreated;

                //RadGrid1.DataBind();
                ScrPlaceHldr.Controls.Add(RadGrid1);
            }

            void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
            {
                if (e.Item is GridFilteringItem)
                {
                    DateTime dtMinVal = new DateTime(0001, 01, 01, 00, 00, 00);
                    DateTime dtMaxVal = new DateTime(9999, 12, 31, 23, 59, 59);

                    GridFilteringItem fitem = (GridFilteringItem)e.Item;
                    foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns)
                    {
                        if (col.ColumnType == "GridDateTimeColumn")
                        {
                            RadDatePicker rp = (RadDatePicker)fitem[col.UniqueName].Controls[1];
                            RadDatePicker rp2 = (RadDatePicker)fitem[col.UniqueName].Controls[4];

                            if (null != rp)
                            {
                                rp.MinDate = dtMinVal;
                                rp.MaxDate = dtMaxVal;
                            }

                            if (null != rp2)
                            {
                                rp2.MinDate = dtMinVal;
                                rp2.MaxDate = dtMaxVal;
                            }
                        }
                    }
                }

            }

            void RadGrid1_PreRender(object sender, EventArgs e)
            {
                //foreach (GridItem item in RadGrid1.MasterTableView.Items)
                //{
                //    if (item is GridEditableItem)
                //    {
                //        GridEditableItem editableItem = item as GridDataItem;
                //        editableItem.Edit = true;
                //    }
                //}

                //RadGrid1.Rebind();
            }

            void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
            {
                RadGrid1.DataSource = createDataTable();
            }

            void createColumn()
            {
                GridBoundColumn boundColumn;
                boundColumn = new GridBoundColumn();
                boundColumn.DataField = "Name";
                boundColumn.HeaderText = "Name";
                boundColumn.UniqueName = "Name";
                boundColumn.HeaderStyle.Width = Unit.Pixel(100);
                boundColumn.FilterControlWidth = Unit.Pixel(80);
                boundColumn.AllowFiltering = true;
                RadGrid1.MasterTableView.Columns.Add(boundColumn);
                

                DateTime dtMinVal = new DateTime(0001, 01, 01, 00, 00, 00);
                DateTime dtMaxVal = new DateTime(9999, 12, 31, 23, 59, 59);


                GridDateTimeColumn boundtimeColumn;
                boundtimeColumn = new GridDateTimeColumn();
                boundtimeColumn.DataField = "Date";
                boundtimeColumn.HeaderText = "Date";
                boundtimeColumn.UniqueName = "Date";
                boundtimeColumn.EnableRangeFiltering = true;
                boundtimeColumn.DataFormatString = "{0:dd/M/yyyy}";
                boundtimeColumn.HeaderStyle.Width = Unit.Pixel(100);
                boundtimeColumn.AllowFiltering = true;
                boundtimeColumn.MinDate = dtMinVal;
                boundtimeColumn.MaxDate = dtMaxVal;
                RadGrid1.MasterTableView.Columns.Add(boundtimeColumn);
            }

            private DataTable createDataTable()
            {
                DataTable dt = new DataTable();

                String strHeader = ("Name,Date");
                String strDataType = ("System.String,System.DateTime");
                String strData = ("Ram,01/05/2002 ,Ram1,01/05/2009,Ram2,01/09/2008");

                int nLength = strHeader.Split(',').Length - 1;
                int nLengthofData = strData.Split(',').Length - 1;
                String[] strHeaderArray = strHeader.Split(',');
                String[] strDataTypeArray = strDataType.Split(',');
                String[] strDataArray = strData.Split(',');


                for (int nStart = 0; nStart <= nLength; nStart++)
                {
                    DataColumn columnString = new DataColumn(strHeaderArray[nStart]);
                    columnString.DataType = System.Type.GetType(strDataTypeArray[nStart]);
                    dt.Columns.Add(columnString);
                }


                for (int nDataStart = 0; nDataStart < nLengthofData; nDataStart = nDataStart + nLength + 1)
                {
                    object[] array = new object[nLength + 1];

                    for (int nStart = 0; nStart <= nLength; nStart++)
                    {
                        array[nStart] = strDataArray[nDataStart + nStart];
                    }

                    dt.Rows.Add(array);

                }
                return dt;
            }

        }
    }

     

    here is the aspx file also

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:PlaceHolder runat="server" ID="ScrPlaceHldr"></asp:PlaceHolder>
        </div>
        </form>
    </body>
    </html>

    Regards,

    Ramesh.

  4. Marin Bratanov
    Admin
    Marin Bratanov avatar
    4489 posts

    Posted 04 Dec 2018 Link to this post

    Hi Ramesh,

    I now understand what you mean. The issue here is that you need to re-set the calendar range on the client as well. I created the following KB for you that demonstrates a solution: https://www.telerik.com/support/kb/aspnet-ajax/grid/details/extend-datetimecolumn-filter-range.

    Based on you provided code, I also advise that you review the following article on properly binding the grid (said shorty, don't set its data source outside of the NeedDataSource handler, and don't call its .DataBind() method): https://www.telerik.com/support/kb/aspnet-ajax/grid/details/how-to-bind-radgrid-properly-on-server-side.


    Regards,
    Marin Bratanov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  5. Ramesh
    Ramesh avatar
    16 posts
    Member since:
    Apr 2015

    Posted 06 Dec 2018 Link to this post

    Hi Marin Bratanov,

    Thank you for your prompted solution. It was nightmare for few days.!

    Thanks again.

    Regards,

    Ramesh.

Back to Top