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

GridDatetimeColumn - Filter by Date Only - Error

3 Answers 104 Views
Grid
This is a migrated thread and some comments may be shown as answers.
July
Top achievements
Rank 2
July asked on 07 May 2012, 03:01 PM
I read a lot of this topic, but now I have an error when add other filter on grid.
this is my scenario

steps:
1- filter by date --> filter is OK.
2- filter by date, filter by colum x--> filter does not work because lost custom date filter.

This is my code:
 public static void FilterDateTimes(RadGrid grid, GridCommandEventArgs e, string columnName, string actionFilter)
        {
            e.Canceled = true;
            var dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe(columnName);
            string newFilter;

            switch (actionFilter)
            {
                case "NoFilter":
                    string removeFilter;
                    if (HttpContext.Current.Session["equalTo"] != null)
                    {
                        removeFilter = HttpContext.Current.Session["equalTo"].ToString();

                        if (grid.MasterTableView.FilterExpression.Contains("AND" + removeFilter))
                            grid.MasterTableView.FilterExpression =
                                grid.MasterTableView.FilterExpression.Replace("AND" + removeFilter, "");

                        if (grid.MasterTableView.FilterExpression.Contains(removeFilter))
                            grid.MasterTableView.FilterExpression =
                                grid.MasterTableView.FilterExpression.Replace(removeFilter, "");
                    }

                    if (HttpContext.Current.Session["NoEqualTo"] != null)
                    {
                        removeFilter = HttpContext.Current.Session["NoEqualTo"].ToString();

                        if (grid.MasterTableView.FilterExpression.Contains("AND" + removeFilter))
                            grid.MasterTableView.FilterExpression =
                                grid.MasterTableView.FilterExpression.Replace("AND" + removeFilter, "");

                        if (grid.MasterTableView.FilterExpression.Contains(removeFilter))
                            grid.MasterTableView.FilterExpression =
                                grid.MasterTableView.FilterExpression.Replace(removeFilter, "");
                    }

                  

                    dateColumn.CurrentFilterFunction = GridKnownFunction.NoFilter;
                    dateColumn.CurrentFilterValue = null;

                    break;

                case "EqualTo":


                    var dt =
                        ((RadDateInput)
                         ((GridFilteringItem)e.Item)[Convert.ToString(columnName)].Controls[1])
                        ;

                    if (dt.Text == null) return ;
                    DateTime date = Convert.ToDateTime(dt.SelectedDate);
                    DateTime startDate = Convert.ToDateTime(date);
                    DateTime endDate = Convert.ToDateTime(date).AddDays(1);

                    newFilter = "('" + startDate.ToString("G") + "' < [Date] AND [Date] < '" +
                                       endDate.ToString("G") + "')";

                     if (HttpContext.Current.Session["equalTo"] != null)
                     {
                         removeFilter = HttpContext.Current.Session["equalTo"].ToString();

                         if (grid.MasterTableView.FilterExpression.Contains("AND" + removeFilter))
                             grid.MasterTableView.FilterExpression =
                                 grid.MasterTableView.FilterExpression.Replace("AND" + removeFilter, "");

                         if (grid.MasterTableView.FilterExpression.Contains(removeFilter))
                             grid.MasterTableView.FilterExpression =
                                 grid.MasterTableView.FilterExpression.Replace(removeFilter, "");
                     }

                    HttpContext.Current.Session["equalTo"] = newFilter;

                    if (grid.MasterTableView.FilterExpression == "")
                    {
                        grid.MasterTableView.FilterExpression = newFilter;
                    }
                    else
                    {
                        if (HttpContext.Current.Session["NoEqualTo"] != null)
                        {
                            removeFilter = HttpContext.Current.Session["NoEqualTo"].ToString();

                            if (grid.MasterTableView.FilterExpression.Contains("AND" + removeFilter))
                                grid.MasterTableView.FilterExpression =
                                    grid.MasterTableView.FilterExpression.Replace("AND" +  removeFilter, "");
                           
                            if (grid.MasterTableView.FilterExpression.Contains(removeFilter))
                                grid.MasterTableView.FilterExpression =
                                    grid.MasterTableView.FilterExpression.Replace(removeFilter, "");
                            
                        }

                        if (grid.MasterTableView.FilterExpression == "")
                        {
                            grid.MasterTableView.FilterExpression = newFilter;
                        }
                        else
                        {
                            grid.MasterTableView.FilterExpression += "AND" + newFilter;
                        }

                    }

                    dateColumn.CurrentFilterValue = Convert.ToDateTime(date).ToString("dd/MM/yyyy");
                    dateColumn.CurrentFilterFunction = GridKnownFunction.EqualTo;

                    break;


                case "NotEqualTo":


                    dt =
                        (RadDateInput)
                        ((GridFilteringItem)e.Item)[Convert.ToString(columnName)].Controls[1];

                    if (dt.Text == null) return ;
                    date = Convert.ToDateTime(dt.SelectedDate);

                    startDate = Convert.ToDateTime(date);
                    endDate = Convert.ToDateTime(date).AddHours(24);
                    DateTime beforeDate = Convert.ToDateTime(date).AddMinutes(-1);

                    newFilter = "([Date] <'" + startDate.ToString("G") + "') OR ([Date] >'" + endDate.ToString("G") + "')";

                    if (HttpContext.Current.Session["NoEqualTo"] != null)
                    {
                        removeFilter = HttpContext.Current.Session["NoEqualTo"].ToString();

                        if (grid.MasterTableView.FilterExpression.Contains("AND" + removeFilter))
                            grid.MasterTableView.FilterExpression =
                                grid.MasterTableView.FilterExpression.Replace("AND" + removeFilter, "");
                        if (grid.MasterTableView.FilterExpression.Contains(removeFilter))
                            grid.MasterTableView.FilterExpression =
                                grid.MasterTableView.FilterExpression.Replace(removeFilter, "");

                    }

                    HttpContext.Current.Session["NoEqualTo"] = newFilter;

                    if (grid.MasterTableView.FilterExpression == "")
                    {
                        grid.MasterTableView.FilterExpression = newFilter;
                    }
                    else
                    {
                        if (HttpContext.Current.Session["equalTo"] != null)
                        {
                            removeFilter = HttpContext.Current.Session["equalTo"].ToString();

                            if (grid.MasterTableView.FilterExpression.Contains("AND" + removeFilter))
                                grid.MasterTableView.FilterExpression =
                                    grid.MasterTableView.FilterExpression.Replace("AND" +  removeFilter, "");
                            if (grid.MasterTableView.FilterExpression.Contains(removeFilter))
                                grid.MasterTableView.FilterExpression =
                                    grid.MasterTableView.FilterExpression.Replace(removeFilter, "");
                            
                        }
                        if (grid.MasterTableView.FilterExpression == "")
                        {
                            grid.MasterTableView.FilterExpression = newFilter;
                        }else
                        {
                            grid.MasterTableView.FilterExpression += "AND" + newFilter;
                        }

                        
                       
                    }
                    dateColumn.CurrentFilterFunction = GridKnownFunction.NotEqualTo;

                    dateColumn.CurrentFilterValue = Convert.ToDateTime(date).ToString("G");
                    break;
            }
            
        }


  if (e.CommandName == RadGrid.FilterCommandName)
            {
                var filterPair = e.CommandArgument as Pair;
                string filter = null;

                if (gridSearchHistory.MasterTableView.FilterExpression.StartsWith("AND"))
                   gridSearchHistory.MasterTableView.FilterExpression = gridSearchHistory.MasterTableView.FilterExpression.Remove(0, 3);

                if (Convert.ToString(filterPair.Second).Contains("Date"))
                {
                    WebSearch.Components.CommonFunctions.GridFunction.FilterDateTimes(gridSearchHistory, e,
                                                                                      Convert.ToString(filterPair.Second),
                                                                                      Convert.ToString(filterPair.First));
                                     
                  }

                LoadDataSource();
            }
        }

Screen:



I have a lot of records with 03/05/2012 as date

Steps 1:
filter as 03/05/2012 --> OK



Step 2:
Add Result equal to 4.



Does not work correctly.

in code behind Filter said:
[Date] = '03/05/2012 12:00:00 a.m. --> LOST the custom filter

My aspx
<telerik:RadGrid ID="gridSearchHistory" runat="server" AutoGenerateColumns="false"  EnableViewState="true"
      OnItemCommand="gridSearchHistory_ItemCommand" OnPageIndexChanged="gridSearchHistory_PageIndexChanged"
    OnPreRender="gridSearchHistory_PreRender"  OnSortCommand="gridSearchHistory_SortCommand" OnPageSizeChanged="gridSearchHistory_PageSizeChanged" >
         <MasterTableView DataKeyNames="Id" CommandItemDisplay="None"
                        TableLayout="Fixed"  Width="100%">
                        <Columns>
                           <telerik:GridBoundColumn UniqueName="Id" DataField="Id" Visible="false" />
                           <telerik:GridDateTimeColumn UniqueName="Date" DataField="Date" PickerType="None"
                                AutoPostBackOnFilter="true" DataType="System.DateTime" />
                       <telerik:GridBoundColumn UniqueName="Query" DataField="Query"  Visible="false" AllowFiltering="false" />
                        <telerik:GridTemplateColumn UniqueName="QueryLink" SortExpression="Query"   AutoPostBackOnFilter="true">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkQuery" runat="server" Text='<%# Eval("Query") %>' OnClick="lnkQuery_Click" />
                            </ItemTemplate>
                         </telerik:GridTemplateColumn>
                           <telerik:GridBoundColumn UniqueName="ResultCount" DataField="ResultCount"   SortExpression="ResultCount"  />
                                                   
                    
                    </Columns>

        </MasterTableView>
    </telerik:RadGrid>

Please I need help

3 Answers, 1 is accepted

Sort by
0
Antonio Stoilkov
Telerik team
answered on 10 May 2012, 11:32 AM
Hello Julieta,

I have examined the provided project and assembled a test page based on it which is attached to the message. Note that when implementing custom filtering you should handle it for all columns and no automatic operations are available. In your case you could set the e.Canceled = true for all filter commands. Custom handling could be done with the help of the session data you are saving. You could take a look at the help article below for more in depth information on the topic:

Kind regards,
Antonio Stoilkov
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
DEVELOPER
Top achievements
Rank 1
answered on 11 Dec 2015, 08:33 PM

Hello Antonio,

 

I tried following the code you post but the following error occurs : When I filter by date and then made a second filtrate by text from another field appears no records to show when if any. Please help me solve this problem.

0
Konstantin Dikov
Telerik team
answered on 16 Dec 2015, 01:04 PM
Hello,

As mentioned in Antonio's post, with custom filtering you need to handle the filter for all columns manually and combine the already applied expressions with the new filters manually as well. This means that you need to build the FilterExpression based on all columns (which is not the case in the attached example in Antonio's post).

Please modify the code accordingly, so it create the FilterExpression based on all columns filter values.


Regards,
Konstantin Dikov
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
July
Top achievements
Rank 2
Answers by
Antonio Stoilkov
Telerik team
DEVELOPER
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or