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

Expand Grid with dynamic columns programmatically; ColumnCreating not fired

1 Answer 165 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Markus
Top achievements
Rank 1
Markus asked on 09 Feb 2009, 03:01 PM

Hello,

I have a grid with a DetailTable and dynamically created columns in that DetailTable. I use the ColumnCreating event to modify the HeaderTexts of the dynamic columns. When I use the standard Expand/Collapse Column everything works fine: the ExpandCommand gets fired, the DetailTable is bound and the ColumnCreating event is fired.

Now I want to automatically expand an item based on conditions that I get from a session variable (which contains the index of the item to expand) without a preceding postback. So in the PreRender Event of the RadGrid I loop through the GridDataItems and if the ItemIndex matches the index retrieved from the session variable I set item.Expanded = true. Now everything seems to work as expected but the ColumnCreating event is not fired hence I can't set the HeaderTexts of the columns.

What am I doing wrong? Expanding an item in the PreRender event only works after a postback. When it's a normal page request the ColumnCreating event isn't fired. I tried to use different events to set the expanded item but with no success.

The grid architecture is pretty complex so it's difficult to post code or a sample project.

Thanks for your help.

Best regards

Sebastian

1 Answer, 1 is accepted

Sort by
0
Accepted
Georgi Krustev
Telerik team
answered on 12 Feb 2009, 09:23 AM
Hi Sebastian,

I post my answer from the support thread opened on this matter. Here is the response there.
Hello Sebastian,

Thank you for contacting us.

To accomplish your task you need to set header text of the columns when Page_Load event is raised. Thus the grid will be able to render the headers properly. OnColumnCreating event is fired only when  custom columns (which inherit from built-in columns) are created. So this is expected behavior. Please refer to this link for more information.

Here is a code snippet showing how to utilize the suggested approach:
    protected override void OnLoad(EventArgs e) 
    { 
        if (!IsPostBack) 
        { 
            CreateDetailTable(); 
            foreach (GridTableView table in RadGrid1.MasterTableView.DetailTables) 
            { 
                if (table.Name == "ChildsTable"
                { 
                    table.Columns[0].HeaderText = "CID Header"
                    table.Columns[1].HeaderText = "ChildName Header"
                } 
            } 
        } 
 
        base.OnLoad(e); 
    } 

Please give it a try and tell me how it goes.
Best,
Georgi Krustev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Markus
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Share this question
or