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

At what point in the server's page/controls lifecycle should I re-order columns

1 Answer 63 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 06 Nov 2014, 12:04 PM
Scenario

I am creating a user control that contains a RadGrid. The user control will have properties that specify the order of the columns in the RadGrid. I plan on using code similar to the snippet taken from http://www.telerik.com/help/aspnet-ajax/grid-reordering-columns.html

GridColumnCollection cols = grid.MasterTableView.Columns;
GridColumn c = cols.FindByUniqueName(columnName);
if (c != null){
    int start = c.OrderIndex;
    for (int i= start; i < cols.Count; i++) 
    {
        c = cols[i];
        if (i < cols.Count - 1)  
            c.OrderIndex = i+1;
        else    
            c.OrderIndex = start; 
    }
}

Question
1. At what point in the pages life cycle should the code be executed? (i.e. page_load, page_init etc)
2. If I do not want to execute the code based on the page lifecycle, are there event(s) based on the RadGrid's life cycle that can be used to execute the code? (i.e. RadGrid1_Init, RadGrid1_Load, RadGrid1_PreRender, RadGrid1_ColumnCreated etc)
3. Any gotcha's I should be aware about?

1 Answer, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 11 Nov 2014, 08:39 AM
Hello David,

An appropriate event to execute the logic from the help article is PreRender and call rebind after that. Note that OrderIndex is not assign earlier and you will not be able to change the column order. Please check out the following code snippet.
protected void Page_PreRender(object sender, EventArgs e)
{
    GridColumnCollection cols = RadGrid1.MasterTableView.Columns;
    GridColumn c = cols.FindByUniqueName("Column2");
    if (c != null)
    {
        int start = c.OrderIndex;
        for (int i = start; i < cols.Count; i++)
        {
            c = cols[i];
            if (i < cols.Count - 1)
                c.OrderIndex = i + 1;
            else
                c.OrderIndex = start;
        }
    }
    RadGrid1.Rebind();
}

Additionally you can check out the following help article which elaborates more on RadGrid's page live cycle.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Share this question
or