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

Problem filtering DateTimeColumn

2 Answers 44 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ferdinand
Top achievements
Rank 1
Ferdinand asked on 08 Feb 2012, 09:37 AM
Hi,
I have a serious problem filtering a GridDateTimeColumn in a dynamically created grid. I tried to correct it following this article:
http://www.telerik.com/help/aspnet/grid/grdfilteringfordatetimecolumnwithdataformatstring.html 
but with no result. I use german culture and maybe that is the cause for the problem.

Is there any known issue with that?

Here is the code for creating the grid:

private void createGrid()
{
    RadGrid grid = new RadGrid();
    this.rgWorkflows = grid;
    setGridProperties(grid);           
}
 
private void setGridProperties(RadGrid grid)
{
    grid.ID = "rgWorkflows";
    grid.EnableViewState = true;
    grid.AutoGenerateColumns = false;
    grid.AllowSorting = true;
    grid.AllowPaging = true;
    grid.AllowFilteringByColumn = false;
    grid.GridLines = GridLines.None;
    grid.Width = Unit.Percentage(99.8);
    grid.Height = Unit.Percentage(99.8);
    grid.HeaderStyle.Width = Unit.Pixel(150);
    grid.AllowMultiRowSelection = true;
    grid.MasterTableView.PagerStyle.Mode = GridPagerMode.NextPrevNumericAndAdvanced;
    grid.ClientSettings.EnableRowHoverStyle = true;
    grid.ClientSettings.Selecting.AllowRowSelect = true;
    grid.ClientSettings.Scrolling.AllowScroll = true;
    grid.ClientSettings.Scrolling.UseStaticHeaders = true;
    grid.ClientSettings.Scrolling.SaveScrollPosition = true;
    grid.ClientSettings.Resizing.AllowColumnResize = true;
    grid.ClientSettings.Resizing.ClipCellContentOnResize = false;
    grid.ClientSettings.Resizing.EnableRealTimeResize = true;
    grid.ClientSettings.Resizing.ResizeGridOnColumnResize = true;
    grid.MasterTableView.DataKeyNames = new string[] { "Id" };
    grid.MasterTableView.OverrideDataSourceControlSorting = true;
    grid.PagerStyle.AlwaysVisible = true;
    if (!IsPostBack)
    {
        grid.MasterTableView.PageSize = 15;
    }
    grid.GroupingSettings.CaseSensitive = false;
    grid.MasterTableView.PagerStyle.Width = 1000;
    grid.MasterTableView.OverrideDataSourceControlSorting = true;
    grid.MasterTableView.EnableColumnsViewState = false;
    grid.AllowFilteringByColumn = true;
 
    grid.AllowMultiRowSelection = false;
 
}


The grid's column are dynamically created like this one:

public static void AddDateTimeFieldToGridView(string fieldName, string headerText, RadGrid grid, int? width)
{
    GridDateTimeColumn dtc = new GridDateTimeColumn()
    {
        HeaderText = headerText,
        DataField = fieldName
    };
    dtc.SortExpression = fieldName.Trim();
    dtc.AllowSorting = true;
    dtc.Resizable = true;
    dtc.UniqueName = fieldName;
    dtc.DataFormatString = "{0:dd.MM.yyyy}";
    dtc.PickerType = GridDateTimeColumnPickerType.DateTimePicker;
    dtc.UniqueName = fieldName;
    if (width != null)
    {
        dtc.HeaderStyle.Width = width.Value;
    }
    grid.Columns.Add(dtc);
}


And the code for the correction I have tried is this (which is executed in ItemCommand event):


private static void setDateFilterPattern(GridCommandEventArgs e, RadGrid grid, string fieldName, string columnUniqueName)
 {
     if (e.CommandName == RadGrid.FilterCommandName
         && ((Pair)e.CommandArgument).Second.ToString() == columnUniqueName
         && ((Pair)e.CommandArgument).First.ToString() != "NoFilter"
         && grid != null)
     {
         e.Canceled = true;
         GridFilteringItem filterItem = (GridFilteringItem)e.Item;
         string currentPattern = (filterItem[((Pair)e.CommandArgument).Second.ToString()].Controls[0] as RadDatePicker).SelectedDate.Value.ToShortDateString();
         string filterPattern = "";
         string filterOption = (e.CommandArgument as Pair).First.ToString();
         GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe(columnUniqueName);               
         filterPattern = currentPattern;
         switch (filterOption)
         {
             case "EqualTo":
                 var dateTime = Convert.ToDateTime(filterPattern);
                 filterPattern = String.Format("[" + fieldName + "] > '{0}' AND [" + fieldName + "] < '{1}'", filterPattern,
                     dateTime.AddDays(1).ToShortDateString());
                 dateColumn.CurrentFilterFunction = GridKnownFunction.EqualTo;
                 break;
             case "NotEqualTo":
                 var dateTime1 = Convert.ToDateTime(filterPattern);
                 filterPattern = String.Format("[" + fieldName + "] < '{0}' OR [" + fieldName + "] > '{1}'", filterPattern,
                     dateTime1.AddDays(1).ToShortDateString());
                 dateColumn.CurrentFilterFunction = GridKnownFunction.NotEqualTo;
                 break;
             case "GreaterThan":
                 filterPattern = "[" + fieldName + "] > '" + filterPattern + "'";
                 dateColumn.CurrentFilterFunction = GridKnownFunction.GreaterThan;
                 break;
             case "LessThan":
                 filterPattern = "[" + fieldName + "] < '" + filterPattern + "'";
                 dateColumn.CurrentFilterFunction = GridKnownFunction.LessThan;
                 break;
             case "GreaterThanOrEqualTo":
                 var dateTime2 = Convert.ToDateTime(filterPattern);
                 filterPattern = String.Format("[" + fieldName + "] > '{0}' AND [" + fieldName + "] < '{1}' OR [" + fieldName + "] >= '{0}'",
                     filterPattern, dateTime2.AddDays(1).ToShortDateString());
                 dateColumn.CurrentFilterFunction = GridKnownFunction.GreaterThanOrEqualTo;
                 break;
             case "LessThanOrEqualTo":
                 var dateTime3 = Convert.ToDateTime(filterPattern);
                 filterPattern = String.Format("[" + fieldName + "] > '{0}' AND [" + fieldName + "] < '{1}' OR [" + fieldName + "] <= '{0}'",
                     filterPattern, dateTime3.AddDays(1).ToShortDateString());
                 dateColumn.CurrentFilterFunction = GridKnownFunction.GreaterThanOrEqualTo;
                 break;
             case "IsNull":
                 filterPattern = "[" + fieldName + "] IS NULL";
                 dateColumn.CurrentFilterFunction = GridKnownFunction.IsNull;
                 break;
             case "NotIsNull":
                 filterPattern = "NOT ([" + fieldName + "] IS NULL)";
                 dateColumn.CurrentFilterFunction = GridKnownFunction.NotIsNull;
                 break;
         }
         foreach (GridColumn column in grid.MasterTableView.Columns)
         {
             if (column.UniqueName != columnUniqueName)
             {
                 column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                 column.CurrentFilterValue = string.Empty;
             }
         }
         dateColumn.CurrentFilterValue = currentPattern;
         grid.MasterTableView.FilterExpression = filterPattern;
 
         filterItem.OwnerTableView.Rebind();
     }
 }

I have to admit that I have to use an old version of Telerik controls (Q3/2010 .NET 2.0) because the project needs to run under Mono.
Any help on this would be appreciated.

Best regards
Ferdinand 

2 Answers, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 13 Feb 2012, 08:46 AM
Hello Ferdinand,

I tried to reproduce the described issue but to no avail. I am sending you a simple example. Please check it out and let me know what differs in your case.

Looking forward for your reply.

Greetings,
Radoslav
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Ferdinand
Top achievements
Rank 1
answered on 13 Feb 2012, 12:01 PM
Thank you Radoslav. I fixed it. The error was caused by not defining the type of the column in my DataSet.
Tags
Grid
Asked by
Ferdinand
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Ferdinand
Top achievements
Rank 1
Share this question
or