Change column index in code

2 posts, 1 answers
  1. Yael Dwir
    Yael Dwir avatar
    4 posts
    Member since:
    Feb 2010

    Posted 25 Oct 2010 Link to this post


    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");

    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.

  2. Answer
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 25 Oct 2010 Link to this post


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

    For more information:
    Reordering columns

Back to Top