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

Problem with RadGrid losing filter options on sort

7 Answers 247 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 01 Jul 2013, 08:37 PM
I am having a strange thing happen when I try to sort a RadGrid.  As soon as I do a sort, the RadComboBox options in my filter under "Market" disappear, and I can't figure out why.  Any thoughts by anybody??

<telerik:RadScriptManager runat="server" />
<telerik:RadAjaxManager runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="gridResults">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="gridResults" UpdatePanelRenderMode="Inline" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
  
<telerik:RadGrid runat="server" ID="gridResults" AutoGenerateColumns="false" AllowFilteringByColumn="true" AllowSorting="true" OnSortCommand="gridResults_OnSortCommand">
    <MasterTableView TableLayout="Fixed">
        <Columns>
            <telerik:GridBoundColumn HeaderText="Loan Number" DataField="LoanNumber" UniqueName="LoanNumber" />
            <telerik:GridBoundColumn HeaderText="FS Market" DataField="FSMarket" AllowFiltering="true" UniqueName="FSMarket">
                <FilterTemplate
                    <telerik:RadComboBox ID="drpdwnFSMarket" runat="server" Width="90" AllowCustomText="false" 
                                DataTextField="FSMarket" DataValueField="FSMarket" CheckBoxes="true"
                                AutoPostBack="true" DropDownWidth="250px" />
                </FilterTemplate>
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>



protected void Page_Load(object sender, EventArgs e)
{
     if (!IsPostBack)
     {
         BindGrid();
         BindGridDropdowns();
     }
 }
 protected void gridResults_OnSortCommand(object o, GridSortCommandEventArgs e)
 {
     BindGrid();
     BindGridDropdowns();
 }
 private void BindGridDropdowns()
 {
     DataTable dt = GetMarketDropdown();
     RadComboBox rcb = (RadComboBox)FindControl("drpdwnFSMarket", gridResults.MasterTableView.Controls);
     rcb.DataSource = dt;
     rcb.DataBind();
     dt.Dispose();
 }
 private void BindGrid()
 {
     DataTable dt = GetResults();
     gridResults.DataSource = dt;
     gridResults.DataBind();
     dt.Dispose();
 }
 private Control FindControl(string uniqueID, ControlCollection cc)
 {
     Control c = null;
     foreach (Control ctrl in cc)
     {
         if (ctrl.ID != null && ctrl.ID == uniqueID)
         {
             c = ctrl;
             break;
         }
         if (ctrl.HasControls())
         {
             c = FindControl(uniqueID, ctrl.Controls);
             if (c != null)
             {
                 break;
             }
         }
     }
     return c;
 }

This seems pretty forward to me?  I bind the grid and then the RadComboBox in the filter.  Everything looks fine.  I do the exact same thing after the sort, and the options are gone. 

Any help would be appreciated!!!

7 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 02 Jul 2013, 04:27 AM
Hi Mike,

I tried to replicate your issue,but no avail. I guess you want to add a FilterTemplate to a column. Please have a look at the below code snippet.

ASPX:
<telerik:RadGrid runat="server" ID="gridResults" AutoGenerateColumns="false" AllowFilteringByColumn="true"
    AllowSorting="true" DataSourceID="SqlDataSource1" >
    <MasterTableView TableLayout="Fixed">
        <Columns>
            <telerik:GridBoundColumn HeaderText="OrderID" DataField="OrderID" UniqueName="LoanNumber" />
            <telerik:GridBoundColumn UniqueName="ShipCity" DataField="ShipCity" HeaderText="ShipCity"
                HeaderStyle-Width="200px">
                <FilterTemplate>
                    <telerik:RadComboBox ID="RadComboBoxCity" DataSourceID="SqlDataSource1" DataTextField="ShipCity"
                        DataValueField="ShipCity" Height="100px" AppendDataBoundItems="true" SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("ShipCity").CurrentFilterValue %>'
                        runat="server" OnClientSelectedIndexChanged="CityIndexChanged">
                        <Items>
                            <telerik:RadComboBoxItem Text="All" />
                        </Items>
                    </telerik:RadComboBox>
                    <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
                        <script type="text/javascript">
                            function CityIndexChanged(sender, args) {
                                var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                tableView.filter("ShipCity", args.get_item().get_value(), "EqualTo");
                            }
                        </script>
                    </telerik:RadScriptBlock>
                </FilterTemplate>
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

Thanks,
Princy
0
Mike
Top achievements
Rank 1
answered on 02 Jul 2013, 05:30 AM

Hmmm....  That doesn't really help me figure out why my code isn't working....

The problem happens when you try to sort the grid.  Actually, it also happens if you use the paging, change the page size, etc. 

I posted an example of it here: http://www.thelukenfamily.com/temp.aspx  Go to that link, and notice that there are 3 filter options inside the FS Market RadComboBox.  Then, try to sort the grid.  If you now go into the filter options, the RadComboBox is empty.  It is almost like the RadComboBox isn't being updated, despite being inside the RadGrid, which has been ajaxified.  Here is the complete source code for that example:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="temp.aspx.cs" Inherits="temp" %>
 
<!DOCTYPE html>
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="gridResults">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="gridResults" UpdatePanelRenderMode="Inline" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
         
        <telerik:RadGrid runat="server" ID="gridResults" AutoGenerateColumns="false" AllowFilteringByColumn="true" AllowSorting="true" OnSortCommand="gridResults_OnSortCommand">
            <MasterTableView TableLayout="Fixed">
                <Columns>
                    <telerik:GridBoundColumn HeaderText="Loan Number" DataField="LoanNumber" UniqueName="LoanNumber" />
                    <telerik:GridBoundColumn HeaderText="FS Market" DataField="FSMarket" AllowFiltering="true" UniqueName="FSMarket">
                        <FilterTemplate
                            <telerik:RadComboBox ID="drpdwnFSMarket" runat="server" Width="90" AllowCustomText="false"
                                                 DataTextField="FSMarket" DataValueField="FSMarket" CheckBoxes="true" DropDownWidth="250px" />
                        </FilterTemplate>
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
 
public partial class temp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
            BindGridDropdowns();
        }
    }
 
    protected void gridResults_OnSortCommand(object o, GridSortCommandEventArgs e)
    {
        BindGrid();
        BindGridDropdowns();
    }
 
    private void BindGridDropdowns()
    {
        DataTable dt = GetMarketDropdown();
        RadComboBox rcb = (RadComboBox)FindControl("drpdwnFSMarket", gridResults.MasterTableView.Controls);
        rcb.DataSource = dt;
        rcb.DataBind();
        dt.Dispose();
    }
 
    private void BindGrid()
    {
        DataTable dt = GetResults();
        gridResults.DataSource = dt;
        gridResults.DataBind();
        dt.Dispose();
    }
 
    private Control FindControl(string uniqueID, ControlCollection cc)
    {
        Control c = null;
 
        foreach (Control ctrl in cc)
        {
            if (ctrl.ID != null && ctrl.ID == uniqueID)
            {
                c = ctrl;
                break;
            }
 
            if (ctrl.HasControls())
            {
                c = FindControl(uniqueID, ctrl.Controls);
                if (c != null)
                {
                    break;
                }
            }
        }
 
        return c;
    }
 
    private DataTable GetResults()
    {
        DataTable dt = new DataTable();
        SqlConnection sqlCN = new SqlConnection(ConfigurationManager.AppSettings["connString"]);
        try
        {
            sqlCN.Open();
            SqlDataAdapter sqlDA = new SqlDataAdapter("SELECT '1234567890' AS LoanNumber, 'Southwest' AS FSMarket", sqlCN);
            sqlDA.Fill(dt);
            sqlDA.Dispose();
        }
        finally
        {
            sqlCN.Close();
            sqlCN.Dispose();
            sqlCN = null;
        }
        return dt;
    }
 
    private DataTable GetMarketDropdown()
    {
        DataTable dt = new DataTable();
        SqlConnection sqlCN = new SqlConnection(ConfigurationManager.AppSettings["connString"]);
        try
        {
            sqlCN.Open();
            SqlDataAdapter sqlDA = new SqlDataAdapter("SELECT 'Southwest' AS FSMarket UNION SELECT 'Northwest' AS FSMarket UNION SELECT 'West' AS FSMarket", sqlCN);
            sqlDA.Fill(dt);
            sqlDA.Dispose();
        }
        finally
        {
            sqlCN.Close();
            sqlCN.Dispose();
            sqlCN = null;
        }
        return dt;
    }
}

This example doesn't have the filters actually wired up, but when they are wired up, the filters work fine.  What I can't figure out - when I sort the grid, I rebind the grid, and I also rebind the filter options.  But it doesn't update them.... 

Any ideas?  Is there another way to add the RadCombiBox to the list of updated controls if that is the issue?????

0
Viktor Tachev
Telerik team
answered on 03 Jul 2013, 02:33 PM
Hello Mike,

I noticed that you are using simple data binding for the RadGrid control. This type of binding is generally used when you do not need the RadGrid to perform complex operations like filtering, grouping, insert/update/delete, etc.

Try using advanced data binding for the RadGrid as it is designed to handle more complex scenarios like the one you would like to implement. You could use the NeedDataSource event or set declarative data source for the RadGrid. You could find an example for RadGrid with enabled filtering and sorting in this online demo. Also an example on setting the datasource for the RadGrid with NeedDataSource could be found here.

Regards,
Viktor Tachev
Telerik
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 the blog feed now.
0
Mike
Top achievements
Rank 1
answered on 03 Jul 2013, 03:01 PM
The example here is really just an example of what is going on.  I stripped everything down to the bare minimum just to show the behavior that we are experiencing.  The actual application does use more complex scenario, which is what is driving us away from NeedDataSource, etc.

Are we stumped here?  Are you able to see the problem we are having with the sort column?  Does anybody have any idea what is causing this, and how to resolve it (workarounds, etc) when utilizing the grid like we are using it?
0
Viktor Tachev
Telerik team
answered on 08 Jul 2013, 06:21 AM
Hi Mike,

Simple data binding (using DataBind() method) for the RadGrid is generally used in simple cases like when you would like to only display some data without anything more.

I am not sure why a complex scenario for the RadGrid would drive you away from advanced data binding. If you would like you could set data source declaratively instead of using NeedDataSource. Both methods for advanced data binding are designed to handle complex operations for the RadGrid like filtering, sorting, paging, etc. As the similarity in the names suggests simple databinding is for simple scenarios and advanced databinding for complex scenarios.

If you need advanced functionality of RadGrid like paging, sorting, grouping, filtering, the only supported way of using it is by implementing advanced data binding for the RadGrid. Give this approach a try and you should be able to get the result you are looking for.

Regards,
Viktor Tachev
Telerik
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 the blog feed now.
0
Mike
Top achievements
Rank 1
answered on 08 Jul 2013, 06:14 PM
Thank you for your help.  It appears that by changing to advanced data binding, I was able to get this to work:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="temp.aspx.cs" Inherits="temp" %>
   
<!DOCTYPE html>
   
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" /> 
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"
            <AjaxSettings
                <telerik:AjaxSetting AjaxControlID="gridResults"
                    <UpdatedControls
                        <telerik:AjaxUpdatedControl ControlID="gridResults" UpdatePanelRenderMode="Inline" />
                    </UpdatedControls
                </telerik:AjaxSetting
            </AjaxSettings
        </telerik:RadAjaxManager
           
        <telerik:RadGrid runat="server" ID="gridResults" AutoGenerateColumns="false" AllowFilteringByColumn="true" AllowSorting="true" OnNeedDataSource="gridResults_OnNeedDataSource" OnItemDataBound="gridResults_OnItemDataBound"
            <MasterTableView TableLayout="Fixed"
                <Columns
                    <telerik:GridBoundColumn HeaderText="Loan Number" DataField="LoanNumber" UniqueName="LoanNumber" /> 
                    <telerik:GridBoundColumn HeaderText="FS Market" DataField="FSMarket" AllowFiltering="true" UniqueName="FSMarket"
                        <FilterTemplate>  
                            <telerik:RadComboBox ID="drpdwnFSMarket" runat="server" Width="90" AllowCustomText="false"
                                                 DataTextField="FSMarket" DataValueField="FSMarket" CheckBoxes="true" DropDownWidth="250px" /> 
                        </FilterTemplate
                    </telerik:GridBoundColumn
                </Columns
            </MasterTableView
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>


using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Data; 
using System.Data.SqlClient; 
using System.Web.UI.WebControls; 
using Telerik.Web.UI; 
     
public partial class temp : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e) 
    
    
     
    protected void gridResults_OnNeedDataSource(object o, GridNeedDataSourceEventArgs e) 
    
        DataTable dt = GetResults(); 
        gridResults.DataSource = dt; 
    
    
    protected void gridResults_OnItemDataBound(object o, GridItemEventArgs e) 
    
        if (e.Item.GetType() == typeof(GridFilteringItem)) 
        
            GridFilteringItem filterItem = e.Item as GridFilteringItem; 
            DataTable dt = GetMarketDropdown(); 
            RadComboBox rcb = (RadComboBox)filterItem.FindControl("drpdwnFSMarket"); 
            rcb.DataSource = dt; 
            rcb.DataBind(); 
        
    
    private DataTable GetResults()     
    
        DataTable dt = new DataTable(); 
        SqlConnection sqlCN = new SqlConnection(ConfigurationManager.AppSettings["connString"]); 
        try 
        
            sqlCN.Open(); 
            SqlDataAdapter sqlDA = new SqlDataAdapter("SELECT '1234567890' AS LoanNumber, 'Southwest' AS FSMarket", sqlCN); 
            sqlDA.Fill(dt); 
            sqlDA.Dispose(); 
        
        finally 
        
            sqlCN.Close(); 
            sqlCN.Dispose(); 
            sqlCN = null; 
        
        return dt; 
    
     
    private DataTable GetMarketDropdown() 
    
        DataTable dt = new DataTable(); 
        SqlConnection sqlCN = new SqlConnection(ConfigurationManager.AppSettings["connString"]); 
        try 
        
            sqlCN.Open(); 
            SqlDataAdapter sqlDA = new SqlDataAdapter("SELECT 'Southwest' AS FSMarket UNION SELECT 'Northwest' AS FSMarket UNION SELECT 'West' AS FSMarket", sqlCN); 
            sqlDA.Fill(dt); 
            sqlDA.Dispose(); 
        
        finally 
        
            sqlCN.Close(); 
            sqlCN.Dispose(); 
            sqlCN = null; 
        
        return dt; 
    
}





0
Viktor Tachev
Telerik team
answered on 10 Jul 2013, 08:39 AM
Hello,

I am glad to hear that you have resolved the issue.

Regards,
Viktor Tachev
Telerik
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 the blog feed now.
Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Mike
Top achievements
Rank 1
Viktor Tachev
Telerik team
Share this question
or