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

[Solved] Dynamic columns in Grid, postback (MasterPage)

1 Answer 206 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel Botero Correa
Top achievements
Rank 1
Daniel Botero Correa asked on 22 Dec 2009, 01:49 PM
Hi everybody,

I try to add Columns to a RadGrid dynamically and I'm able to do it, but, when i do a post back
the RadGrid's columns are reinitialized and i don't want it. i want that my RadGrid stay
as it was before postback. Why? i want to save the columns' position and if the RadGrid
is reinitialized, I can't it.

Example:

RadGrid Status (initial):

Column1 Column2 Column3

User changes the columns' order:

Column3 Column2 Column1

User does post back and RadGrid come back to status initial.

Column1 Column2 Column3

If i do that in a page aspx (which not inherits of a masterpage), my code works, but
with a masterPage doesnt work. someone has an idea?

It's a very simple thing but i tried to do it and I'm not be able, i read all forums and i do all, but that not works in a page which inherits of a master page.

Someone can help me?

My RadGrid's code:
                                <telerik:RadGrid ID="RadGrid2" runat="server" AllowPaging="false"  
                                    EnableLinqExpressions="false" GridLines="None"  AutoGenerateColumns="false"  
                                        Skin="InductDefault" EnableEmbeddedSkins="false"  
                                        onneeddatasource="RadGrid2_NeedDataSource"
                                    <MasterTableView AutoGenerateColumns="False" EnableNoRecordsTemplate="false"  
                                        HierarchyDefaultExpanded="false" HierarchyLoadMode="Client" Width="100%"
                                    </MasterTableView> 
                                    <ClientSettings AllowColumnHide="true" AllowColumnsReorder="true"  
                                        ReorderColumnsOnClient="true"
                                    </ClientSettings> 
                                </telerik:RadGrid> 

C# code:

    protected void page_Init(object sender, EventArgs e) 
    { 
        if (!IsPostBack) 
        { 
            createRadGrid2(); 
        } 
    } 
 
    public void createRadGrid2() 
    { 
        XDocument xmlDoc = XDocument.Load(RadGridXmlConfig); 
        var Columns = xmlDoc.Elements("Columns"
                              .Elements("Column"
                              .Attributes("uniqueName"); 
 
        foreach (var colTemp in Columns) 
        { 
            String uniqueName = colTemp.Value; 
            tableTripsDetail.Columns.Add(uniqueName); 
            RadGrid2.MasterTableView.Columns.Add(createBoundColumn(uniqueName)); 
        } 
    } 
 
    protected void RadGrid2_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
    { 
        RadGrid2.DataSource = getTableReportTripsDetail(); 
    } 
 
    private GridBoundColumn createBoundColumn(String valueUnique) 
    { 
        GridBoundColumn BoundColumn = new GridBoundColumn(); 
        BoundColumn.DataField = valueUnique; 
        BoundColumn.HeaderText = valueUnique; 
        BoundColumn.SortExpression = valueUnique; 
        BoundColumn.UniqueName = valueUnique; 
 
        return BoundColumn; 
    } 

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 28 Dec 2009, 02:24 PM
Hi Daniel,

Have you, by any chance, disabled ViewState in the master page? I cannot reproduce this behavior. Particularly, I can see no difference between using and not using a master page.

Greetings,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Daniel Botero Correa
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or