RadGrid Filtering error when creating columns dynamically

1 Answer 189 Views
Filter Grid
Gregory
Top achievements
Rank 2
Gregory asked on 01 Feb 2022, 09:02 PM

Hello,

I have a scenario where I would like to create columns dynamically and have filters on that grid. So far, when I set the AllowFilteringByColumn to true and create the column dynamically at the OnNeedDataSource event. I receive an error when I try to filter "Expression Expected".

If I take that same column and declare it aspx page, I can filter no problem.

<telerik:RadGrid ID="rgPortfolio" runat="server" 
    AllowFilteringByColumn="true" MasterTableView-AllowPaging="true" PagerStyle-AlwaysVisible="true"
    AllowSorting="true" AllowPaging="true" AutoGenerateColumns="false" 
    OnNeedDataSource="rgPortfolio_NeedDataSource"
    OnColumnCreated="rgPortfolio_ColumnCreated">
    <MasterTableView AllowFilteringByColumn="true" DataKeyNames="Id">
        <Columns>
            <%--<telerik:GridBoundColumn UniqueName="Cusip" DataField="Cusip"></telerik:GridBoundColumn>--%>
        </Columns>
    </MasterTableView>
                
</telerik:RadGrid>


private void BuildGridColumns()
{
        
    rgPortfolio.Columns.Clear();
    var col = new GridBoundColumn();
    col.UniqueName = "cusip";
    col.HeaderText = "cusip";
    col.DataField = "cusip";
    rgPortfolio.MasterTableView.Columns.Add(col);

}

protected void rgPortfolio_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    BuildGridColumns();
    rgPortfolio.DataSource = Reporting.Investor.GetActivePortfolioItems();
}

1 Answer, 1 is accepted

Sort by
0
Doncho
Telerik team
answered on 04 Feb 2022, 11:30 AM

Hi Gregory,

The NeedDataSource event of the RadGrid is designed only for the purpose of data binding as described in the Using the NeedDataSource event to connect data. This event is too late for changing/creating the RadGrid structure and instead you should use an earlier one, like the Page_Load or Page_Init.

If you need to generate the entire structure of the RadGrid or just to define its columns in the code behind you should follow the instructions in the Creating a RadGrid Programmatically.

I hope you will find this information helpful.

Kind regards,
Doncho
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Gregory
Top achievements
Rank 2
commented on 07 Feb 2022, 05:14 PM

Hi Doncho,

Thanks for the answer. I tried to place the the building of the columns in many different places between Page_OnLoad or Page_PreInit or RadGrid_PreRender but if I created the columns dynamically and the filters dynamically, i would still get the same error.

I ended up going with a slightly reversed approach where I created every single column a user might can include in the declaration of the grid and then dynamically exclude those excluded by the user. This worked for me.

Thank you

Gregory

Tags
Filter Grid
Asked by
Gregory
Top achievements
Rank 2
Answers by
Doncho
Telerik team
Share this question
or