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

Programmatically Created Columns and Dropdown Filters

8 Answers 160 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ben
Top achievements
Rank 1
Ben asked on 15 Dec 2011, 08:55 PM

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

8 Answers, 1 is accepted

Sort by
0
Antonio Stoilkov
Telerik team
answered on 20 Dec 2011, 04:36 PM
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
0
Ben
Top achievements
Rank 1
answered on 08 Feb 2012, 04:35 PM
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
0
Antonio Stoilkov
Telerik team
answered on 13 Feb 2012, 02:39 PM
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 >>
0
Ben
Top achievements
Rank 1
answered on 27 Feb 2012, 08:49 PM
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
0
Antonio Stoilkov
Telerik team
answered on 01 Mar 2012, 03:17 PM
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.
0
Brandon R.
Top achievements
Rank 1
answered on 05 Dec 2014, 03:13 PM
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).
0
John
Top achievements
Rank 1
answered on 23 Oct 2019, 12:26 AM
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.
0
John
Top achievements
Rank 1
answered on 23 Oct 2019, 04:19 PM
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.
Tags
Grid
Asked by
Ben
Top achievements
Rank 1
Answers by
Antonio Stoilkov
Telerik team
Ben
Top achievements
Rank 1
Ben
Top achievements
Rank 1
Brandon R.
Top achievements
Rank 1
John
Top achievements
Rank 1
Share this question
or