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

Dynamic Column Creation

1 Answer 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sanjib
Top achievements
Rank 1
Sanjib asked on 06 Dec 2012, 05:03 AM
Hi,

I am trying to generated BoundColumns dynamically, i have read the documentation (http://www.telerik.com/help/aspnet-ajax/grid-using-columns.html) and the programmatic example (http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html), and drafted the following code, but nothing is getting rendered in the page, could you please help,

I debugged and found that the columns collection has the columns as the way I added them but they are not rendered in my page.
Please note I have checked in IE9 / FF16 and using IIS7.5 & net framework 4.0

Find below my code....

aspx,

<telerik:RadGrid ID="RadGridStores" runat="server" AutoGenerateColumns="False" EnableViewState="False" CellSpacing="0" GridLines="Both">
                                            <ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True">
                                                <Selecting AllowRowSelect="True" />
                                                <ClientEvents OnGridCreated="RadGridStores_OnGridCreated"/>
                                            </ClientSettings>
                                            <MasterTableView BorderStyle="Double" EnableViewState="False">
                                                <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
                                                <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                                                    <HeaderStyle Width="20px"></HeaderStyle>
                                                </RowIndicatorColumn>
                                                <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                                                    <HeaderStyle Width="20px"></HeaderStyle>
                                                </ExpandCollapseColumn>
                                                <EditFormSettings>
                                                    <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
                                                </EditFormSettings>                                                
                                            </MasterTableView>
                                            <FilterMenu EnableImageSprites="False"></FilterMenu>
                                        </telerik:RadGrid>

aspx.cs

private void SetUpDynamicColumns(UserAppletSettings usrAppConfig, RadGrid grid)
    {
        if (usrAppConfig.ItemCount <= 0) { return; }

        Telerik.Web.UI.GridBoundColumn gbCol = null;
        List<UsersDisplayColumnPKL> _enItems = null;
        try
        {
            _enItems = new List<UsersDisplayColumnPKL>(usrAppConfig.ItemByColumnOrder.Values);
            _enItems.Sort((UsersDisplayColumnPKL l, UsersDisplayColumnPKL r) => l.ColumnOrder.CompareTo(r.ColumnOrder));
            _enItems = _enItems.FindAll((UsersDisplayColumnPKL item) => item.ColumnVisible != 0 ||
                (item.ColumnVisible == 0 && item.ColumnOrder >= 999 && item.ColumnHidden == 0));

            for (int index = 0, length = _enItems.Count; index < length; index++)
            {
                gbCol = new Telerik.Web.UI.GridBoundColumn();
                grid.MasterTableView.Columns.Add(gbCol);

                gbCol.Visible = true;
                gbCol.HeaderText = _enItems[index].ColumnHeader;
                gbCol.DataField = _enItems[index].ColumnName;
                gbCol.UniqueName = _enItems[index].ColumnName;
               
            }

        }
        finally
        {
            if (!ReferenceEquals(_enItems, null))
            {
                _enItems.Clear();
                _enItems = null;
            }
        }
    }


protected void Page_Load(object sender, EventArgs e)
    {
        // Pre-condition section
        if (IsPostBack) return;
        if (IsCallback) return;

        SetCurrentTab();

        SetUpDynamicColumns(new UserAppletSettings(WebDataKeys.SCR_OFSS, UserAppletSettings.EAppletType.STOR, this.RadGridStores.MasterTableView),
            this.RadGridStores);

    }

Regards
Sanjib Harchowdhury

1 Answer, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 10 Dec 2012, 10:19 AM
Hello,

 Can you please try enabling the ViewState for the grid. Otherwise the columns have to be added every time on Page_Load (without conditional check) or on Page_Init.

Greetings,
Marin
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.
Tags
Grid
Asked by
Sanjib
Top achievements
Rank 1
Answers by
Marin
Telerik team
Share this question
or