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

Programmatic creation of columns in RadGrid

3 Answers 108 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
mdanna
Top achievements
Rank 1
mdanna asked on 29 Jun 2010, 01:40 PM
Hi,

I'm having an issue with adding columns to the radgrid programmatically.  I'm adding GridTemplateColumns and doing it in Page_Init.  I have read the documentation and have realized that I need to add them to the ColumnCollection before I set the properties so I am doing this also.  Everything displays properly if(!Postback), but when I click to sort, refresh, page, etc, the content that I had displayed inside the grid remains, but a duplicate column is inserted for each column and the content in that column is blank.  I have tried moving and taking out adding the new columns in the Page_Load, OnPreRender, Page_Init, and with if(!Postback) in each case, but I am not having any luck. 

Is there something small that I'm missing that you can help me out with?

Thanks in advance,
Matt

3 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 29 Jun 2010, 01:51 PM
Hi mdanna,

Please verify that you follow strictly the conventions for dynamic grid structure creation explained here and the problem should be elminated. It seems that in your case the grid viewstate is not persisted properly which causes the unwanted side effect.

Best regards,
Sebastian
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
mdanna
Top achievements
Rank 1
answered on 29 Jun 2010, 02:15 PM
This is my code which is exactly what the link you sent me details.  It is fairly straightforward, but I am getting too many columns.  Do you see any problem with it?  ViewState is allowed for the page, grid, mastertableview.

protected
void Page_Init(object sender, EventArgs e)

{

 

 

    this.InitDataTable();

 

 

    this.AddColumnsToGrid();

 

}

 

protected override void OnPreRender(EventArgs e)

 

{

 

    base.OnPreRender(e);

 

 

    if (!this.Page.IsPostBack)

 

    {

 

        this.SetCrossTabStatusDataSource();

 

 

        this.CrossTabStatusGrid.DataBind();

 

    }

}

 

protected void InitDataTable()

 

{

 

    this.DataTable = ForecastCrossTabStatusHelper.GetForecastCrossTabStatusDataSource();

 

}

 


protected
void AddColumnsToGrid()

 

{
    

foreach(DataColumn column in this.DataTable.Columns)

 

    {

 

        GridTemplateColumn gridCol = new GridTemplateColumn();

 

 

        this.CrossTabStatusGrid.MasterTableView.Columns.Add(gridCol);

 

        gridCol.HeaderText = column.ColumnName;

        gridCol.UniqueName = column.ColumnName;

        gridCol.DataField = column.ColumnName;

        gridCol.ItemTemplate =

new GeographyItemTemplate(column);

 

        gridCol.SortExpression = column.ColumnName;

    }

}

 

protected void SetCrossTabStatusDataSource()

 

{

 

    this.CrossTabStatusGrid.DataSource = this.DataTable;

 

}

 

protected void CrossTabStatusGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)

 

{

 

    this.InitDataTable();

 

 

    this.SetCrossTabStatusDataSource();

 

}

0
mdanna
Top achievements
Rank 1
answered on 29 Jun 2010, 02:23 PM
I was able to get this to work correctly by turning the ViewState off on the page.  Working perfectly now, but thanks for your help.
Tags
General Discussions
Asked by
mdanna
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
mdanna
Top achievements
Rank 1
Share this question
or