Programmatically Created Columns and Dropdown Filters

9 posts, 0 answers
  1. Ben
    Ben avatar
    3 posts
    Member since:
    Dec 2011

    Posted 15 Dec 2011 Link to this post

    Hello,
    I am binding columns dynamically to my grid as per this article:
    http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html#Section21

    I am also attempting to implement a drop-down filter box as per this article:
    http://www.telerik.com/help/aspnet-ajax/grid-filtering-with-dropdownlist.html

    But when I postback the the grid for any reason (refresh, paging, or applying a filter), I get the following exception:

    Cannot create column with the specified type name: CustomFilteringColumn

    How can I make this programatic column (instanciated at Page_Load) play nice with the column creation on post-backs?

    here is my class that inherits from GridBound Column (as per the 2nd article):

        public class CustomFilteringColumn : GridBoundColumn
        {
     
            protected override void SetupFilterControls(System.Web.UI.WebControls.TableCell cell)
            {
                RadComboBox rcBox = new RadComboBox();
                rcBox.ID = "ddlFilter_" + Guid.NewGuid().ToString();
                rcBox.AutoPostBack = true;
                rcBox.DataTextField = this.DataField;
                rcBox.DataValueField = this.DataField;
                rcBox.SelectedIndexChanged += rcBox_SelectedIndexChanged;
     
                RadComboBoxItem blankItem = new RadComboBoxItem(string.Empty, string.Empty);
                rcBox.Items.Add(blankItem);
     
                // add other items to the dropdown here
     
                cell.Controls.Add(rcBox);
            }
     
            protected override void SetCurrentFilterValueToControl(System.Web.UI.WebControls.TableCell cell)
            {
                base.SetCurrentFilterValueToControl(cell);
            }
     
            protected override string GetCurrentFilterValueFromControl(System.Web.UI.WebControls.TableCell cell)
            {
                return base.GetCurrentFilterValueFromControl(cell);
            }
     
            private void rcBox_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
            {
                ((GridFilteringItem)(((RadComboBox)sender).Parent.Parent)).FireCommandEvent("Filter"new System.Web.UI.Pair());
            }
        }

    Thanks,
    Ben

  2. Antonio Stoilkov
    Admin
    Antonio Stoilkov avatar
    530 posts

    Posted 20 Dec 2011 Link to this post

    Hello Ben,

    You could use programmatic creation for the custom filtering column. Note that the exception you are experiencing is because RadGrid does not support mixing declarative grid columns with grid columns added dynamically at runtime. You should either create all the columns in the grid programmatically, or else define them all in the ASPX file.
    CustomFilteringColumn customFilteringColumn = new CustomFilteringColumn();
    customFilteringColumn.DataField = "CustomFilteringColumnDataField";
    customFilteringColumn.HeaderText = "CustomFilteringHeaderText";
    RadGrid1.MasterTableView.Columns.Add(boundColumn);

    Greetings,
    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
  3. Ben
    Ben avatar
    3 posts
    Member since:
    Dec 2011

    Posted 08 Feb 2012 Link to this post

    Hi Antonio,

    Thanks for the reply!

    I am creating all columns programatically, here is my grid in the aspx file:
    <telerik:RadAjaxPanel ID="pnlGridMasterPanel" runat="server" LoadingPanelID="RadAjaxLoadingPanel1">
        <telerik:RadGrid ID="grdEntity" runat="server">
        </telerik:RadGrid>
    </telerik:RadAjaxPanel>

    And here is how I add it (looks almost the exact same as your sample:
    CustomFilteringColumn boundColumn;
    //Important: first Add column to the collection
    boundColumn = new CustomFilteringColumn (CustomFilterParameters, pstrDataField);
    this.grdEntity.MasterTableView.Columns.Add(boundColumn);
    //Then set properties
    boundColumn.DataField = pstrDataField;
    boundColumn.HeaderText = pstrHeaderText;

    I have a constructor with parameters, is that possibly the problem? 

    All the other columns on the grid work perfectly with the GridBoundColumn class in Telerik.Web.UI.

    Thanks again,
    Ben
  4. Antonio Stoilkov
    Admin
    Antonio Stoilkov avatar
    530 posts

    Posted 13 Feb 2012 Link to this post

    Hi Ben,

    I have examined your code and noticed that you are passing CustomFilterParameters and pstrDataField to the constructor of the column which internally could set these properties. As you state in the comments it is required first to add the column to the columns collection and then set the properties. In order to resolve this issue you could remove the declaration of these properties in the constructor of CustomFilteringColumn.

    Greetings,
    Antonio Stoilkov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  5. Ben
    Ben avatar
    2 posts
    Member since:
    Feb 2012

    Posted 27 Feb 2012 Link to this post

    Hi Antonio,

    I removed all parameters from the constructor and I still get the javascript exception:

    A first chance exception of type 'Telerik.Web.UI.GridException' occurred in Telerik.Web.UI.DLL
    A first chance exception of type 'System.Web.HttpUnhandledException' occurred in System.Web.dll
    A first chance exception of type 'System.Web.HttpUnhandledException' occurred in System.Web.dll
    Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Cannot create column with the specified type name: OptionsetFilteringColumn


    In hopes of adding more information: if I set a breakpoint in the constructor and another breakpoint in the override for SetupFilterControls neither will hit on the postback. I am inferring that it is blocking the creation of this OptionsetFilteringColumn because somewhere in the internals of the grid it needs to know about my custom grid class?

    Thanks,
    Ben
  6. Antonio Stoilkov
    Admin
    Antonio Stoilkov avatar
    530 posts

    Posted 01 Mar 2012 Link to this post

    Hi Ben,

    You could resolve your issue by adding column templates in the Page_Init event handler, so that the template controls can be added to the ViewState as it is stated in Creating template columns programmatically section in the help article below:

    Additionally, we could advise you to use the approach shown in the demo below. Your current implementation was used when no FilterTemplate existed and is harder to create and maintain.

    All the best,
    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.
  7. Brandon R.
    Brandon R. avatar
    2 posts
    Member since:
    Dec 2014

    Posted 05 Dec 2014 in reply to Ben Link to this post

    Had exactly this problem, and scoured the nets for a while on this one.  Add this attribute to your MasterTableView: EnableColumnsViewState="False".  In the codebehind, you might have to call RadGrid.Rebind() on IsPostBack == true in Page_Init/Page_Load in order to get the data and columns to show up (you're manually rebinding instead of letting the viewstate do it).
  8. John
    John avatar
    6 posts
    Member since:
    Jul 2015

    Posted 22 Oct 2019 in reply to Brandon R. Link to this post

    I have this same problem but setting the EnableColumnsViewState property of the MasterTableView just makes the whole grid disappear. My problem may be compounded by the fact that everything is happening in a custom user control.
  9. John
    John avatar
    6 posts
    Member since:
    Jul 2015

    Posted 23 Oct 2019 in reply to John Link to this post

    I did finally get it to work in the user control by implementing the Page_Init and building the grid from there with the EnableColumnViewState set to false on the MasterTableView property of the grid. So, pretty much as indicated earlier by others.
Back to Top