Grid filtering not working correctly

2 posts, 0 answers
  1. Steve
    Steve avatar
    1 posts
    Member since:
    Jun 2015

    Posted 13 Jan 2020 Link to this post

    I hate to bring this question to you but I don't understand your RadGrid filtering example and have coded it the best I can. It is a simple example that has a DataTable loaded as the grid datasource on page load. Now the both the ASPX and the ASPX.CS code is below:

    ASPX.CS

        public partial class AdminSearch : System.Web.UI.Page
        {
            public static ArrayList LocationArray;

            public DataTable myDataTable = new DataTable();

            SqlDataSource sqlDS = new SqlDataSource();

            protected void Page_Load(object sender, EventArgs e)
            {
                    SearchGrid.DataSource = GetDataTable();
            }

            public DataTable GetDataTable()
            {


                myDataTable.Columns.Add(new DataColumn("KMLID", typeof(string)));
                myDataTable.Columns.Add(new DataColumn("Remarks", typeof(string)));
                myDataTable.Columns.Add(new DataColumn("LONGITUDE", typeof(string)));
                myDataTable.Columns.Add(new DataColumn("LATITUDE", typeof(string)));
                myDataTable.Columns.Add(new DataColumn("Pax", typeof(string)));
                myDataTable.Columns.Add(new DataColumn("StartDate", typeof(string)));
                myDataTable.Columns.Add(new DataColumn("EndDate", typeof(string)));
                myDataTable.Columns.Add(new DataColumn("Type", typeof(string)));

                DataRow workRow;
                
               bool bsuccess = CacheManager.Get<ArrayList>("LocationArray", out LocationArray);
                foreach (Marker m in LocationArray)
                {
                    
                    workRow = myDataTable.NewRow();
                    workRow["KMLID"] = m.KMLID;
                    workRow["Remarks"] = m.Remarks;
                    workRow["LONGITUDE"] = m.LONGITUDE;
                    workRow["LATITUDE"] = m.LATITUDE;
                    workRow["Pax"] = m.Pax;
                    workRow["StartDate"] = m.StartDate.ToString();               
                    workRow["EndDate"] = m.EndDate.ToString();

                     if (m.Type == "Operations")
                    {
                        workRow["Type"] = m.Type;
                    }
                    else
                    {
                        workRow["Type"] = m.Type;
                    }

                    myDataTable.Rows.Add(workRow);
                }

                return myDataTable;
        
            }


            protected void SearchGrid_PreRender(object sender, System.EventArgs e)
            {
                if (SearchGrid.MasterTableView.FilterExpression != string.Empty)
                {
                    SearchGrid.DataSource = GetDataTable(); 
                }
            }

            protected void SearchGrid_ItemCommand(object source, GridCommandEventArgs e)
            {
                myDataTable.Clear();
                if (e.CommandName == RadGrid.FilterCommandName)
                {
                    Pair filterPair = (Pair)e.CommandArgument;

                    switch (filterPair.Second.ToString())
                    {
                        case "StartDate":
                            this.startDate = ((e.Item as GridFilteringItem)[filterPair.Second.ToString()].FindControl("StartDatePicker") as RadDatePicker).SelectedDate;
                            this.endDate = ((e.Item as GridFilteringItem)[filterPair.Second.ToString()].FindControl("StartDatePicker") as RadDatePicker).SelectedDate;
                            break;
                       default:
                            break;
                    }                
                }
            }
            protected DateTime? SetStartDate(GridItem item)
            {
                if (item.OwnerTableView.GetColumn("StartDate").CurrentFilterValue == string.Empty)
                {
                    return new DateTime?();
                }
                else
                {
                    return DateTime.Parse(item.OwnerTableView.GetColumn("StartDate").CurrentFilterValue);
                }
            }
            protected DateTime? startDate
            {
                set
                {
                    ViewState["strD"] = value;
                }
                get
                {
                    if (ViewState["strD"] != null)
                        return (DateTime)ViewState["strD"];
                    else
                        return new DateTime(2019, 7, 4);
                }
            }
            protected DateTime? endDate
            {
                set
                {
                    ViewState["endD"] = value;
                }
                get
                {
                    if (ViewState["endD"] != null)
                        return (DateTime)ViewState["endD"];
                    else
                        return new DateTime(2019, 5, 6);
                }
            }
        }

    ASPX

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="AdminSearch.aspx.cs" Inherits="COPASPApplication.AdminSearch" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        <div>
            <span style="align-content:end; font-size:12px;"><asp:LinkButton runat="server" PostBackUrl="COPAdminView.aspx">Administrative User Page</asp:LinkButton></span>
            <h3>Administrative Search Page</h3>
        </div>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
        <div class="demo-container no-bg">
            <telerik:RadGrid RenderMode="Lightweight" ID="SearchGrid" Width="100%" AllowFilteringByColumn="True"
                AllowSorting="True" AllowPaging="True" PageSize="7" runat="server" AutoGenerateColumns="False"
                EnableLinqExpressions="false" OnItemCommand="SearchGrid_ItemCommand" ShowStatusBar="true">
                <MasterTableView DataKeyNames="KMLID" TableLayout="Fixed">
                    <Columns>
                         <telerik:GridBoundColumn DataField="KMLID" HeaderText="KMLID" UniqueName="KMLID"
                            HeaderStyle-Width="25px" SortExpression="KMLID">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Remarks" HeaderText="Remarks" UniqueName="Remarks"
                            HeaderStyle-Width="100px" SortExpression="Remarks">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="LONGITUDE" HeaderText="LONGITUDE" UniqueName="LONGITUDE"
                            HeaderStyle-Width="80px" SortExpression="LONGITUDE">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="LATITUDE" HeaderText="LATITUDE" UniqueName="LATITUDE"
                            HeaderStyle-Width="80px" SortExpression="LATITUDE">
                         </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Pax" HeaderText="Pax" UniqueName="Pax"
                            HeaderStyle-Width="25px" SortExpression="Pax" >
                        </telerik:GridBoundColumn>

                        <telerik:GridBoundColumn UniqueName="StartDate" DataField="StartDate" HeaderText="Start date"
                            DataFormatString="{0:D}" HeaderStyle-Width="90px">
                            <FilterTemplate>
                            <telerik:RadDatePicker RenderMode="Lightweight" ID="StartDatePicker" runat="server" Width="110px" ClientEvents-OnDateSelected="StartDateSelected"
                                MinDate="01-01-2019" MaxDate="05-06-2025" FocusedDate="07-04-1996" />                              
                                <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
                                    <script type="text/javascript">
                                        function StartDateSelected(sender, args) {
                                            var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                        var startDate = $find('<%# ((GridItem)Container).FindControl("StartDatePicker").ClientID %>');
                                            var startDate = FormatSelectedDate(sender);
                                            tableView.filter("StartDate", startDate, "EqualTo"); 
     
                                    }
                                    function FormatSelectedDate(picker) {
                                        var date = picker.get_selectedDate();
                                        var dateInput = picker.get_dateInput();
                                        var formattedDate = dateInput.get_dateFormatInfo().FormatDate(date, dateInput.get_displayDateFormat());
     
                                        return formattedDate;
                                    }
                                    </script>
                                </telerik:RadScriptBlock>
                            </FilterTemplate>
                        </telerik:GridBoundColumn>

                        <telerik:GridBoundColumn UniqueName="EndDate" DataField="EndDate" HeaderText="EndDate"
                            DataFormatString="{0:d}" HeaderStyle-Width="80px">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Type" HeaderText="Type" UniqueName="Type"
                            HeaderStyle-Width="80px" SortExpression="Type">
                        </telerik:GridBoundColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>
        </div>
    </asp:Content>

     

  2. Attila Antal
    Admin
    Attila Antal avatar
    591 posts

    Posted 16 Jan 2020 Link to this post

    Hi Steve,

    The filtering mechanism of the Grid is not that complex as it sounds, but factors like data binding can influence it. For instance, binding data to RadGrid should not be done in the Page Load or Page_PreRender events. I know, that the generic ASP GridView has only the DataBind() method and you need to use this every time the data changes, without paying special attention, but RadGrid is a advanced version of that which comes with its own DataBinding mechanism that we recommend instead of the DataBind() or DataSource.

    The RadGrid should be using the NeedDataSource event, or using Declarative DataSource controls. This is also described in the How to bind RadGrid properly on server-side

    Here is a very simple example that you can add to a new ASPX page and test the filtering:

    <telerik:RadGrid ID="RadGrid1" runat="server" Width="800px" AllowFilteringByColumn="true" AllowPaging="true" OnNeedDataSource="RadGrid1_NeedDataSource">
    </telerik:RadGrid>

    C# - Binding data. Note that this example uses an Enumerable and and not DataTable, however, it doesn't matter, as soon as the Grid has the data, it filters on that and not the DataTable or other data sources.

    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = Enumerable.Range(1, 100).Select(x => new
        {
            OrderID = x,
            OrderDate = DateTime.Now.Date.AddHours(x),
            Freight = x * .1,
            ShipName = "Name " + x,
            ShipCountry = "Country " + x
        });
    }

     

    Kind regards,
    Attila Antal
    Progress Telerik

    Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
Back to Top