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

Change column index in code

1 Answer 94 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Yael Dwir
Top achievements
Rank 1
Yael Dwir asked on 25 Oct 2010, 09:28 AM
Hello,

I want to change the column order in code (in Page_Load).

i write this code:
    
    GridColumn gc1 = MyGrid.Columns.FindByDataField("ID");
    GridColumn gc2 = MyGrid.Columns.FindByDataField("NAME");
    MyGrid.Columns.Remove(gc1);
    MyGrid.Columns.Remove(gc2);
    MyGrid.Columns.AddAt(0,gc2);
    MyGrid.Columns.AddAt(0,gc1);

with this code i took "ID" column and "NAME" column and put them right in the start.
When the page is loaded in the first time, everything look good, the "ID" is at the start and the "Name" is right after.
when i click on the grid or make any postback to the server i get this error:

"Sys.WebForms.PageRequestManagerServerException: Failed to load viewstate. the control tree into which viewsate being loaded must match the control tree that was used to save viewstate during the previous request. for example, when adding controls dynamically the controls added during a post back must match the type and position of the control added during the initial request

Can you please help me with changed index spot during page_load event?
I have no idea what to do with this error.


Yael.

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 25 Oct 2010, 11:21 AM
Hello,

I hope setting the OrderIndex for columns would be better option to change the column order.


For more information:
Reordering columns



Thanks,
Princy.
Tags
Grid
Asked by
Yael Dwir
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or