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

Adding columns in Page_Load

3 Answers 49 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jay
Top achievements
Rank 1
Jay asked on 02 Jul 2008, 07:16 PM
I tracked down a problem after a few hours of digging ...

I am creating columns in the Page_Load event for a RadGrid. I only add the columns if NOT a PostBack event. Basically I had something like this:

if (!Page.IsPostBack)
{
 // Add each column to the DataTable
 for (int col = 0; col < kview.GridColumns.Count; col++)
 {
  GridBoundColumn boundColumn;
  boundColumn = new GridBoundColumn();
  boundColumn.UniqueName = "Name" + col;
  boundColumn.DataField = "Data" + col;
  boundColumn.HeaderText = "Header" + col;
 // NOTE: I had the .Add AFTER setting the properties
  this.RadGrid1.MasterTableView.Columns.Add(boundColumn);
 }
}

The problem was any postback cleared the column names. Then I found the article
http://www.telerik.com/help/aspnet/grid/grdprogrammaticcreation.html
which said to set the properties AFTER adding the column. So I changed it to this:

if (!Page.IsPostBack)
{
 // Add each column to the DataTable
 for (int col = 0; col < kview.GridColumns.Count; col++)
 {
  GridBoundColumn boundColumn;
  boundColumn = new GridBoundColumn();
// NOTE: Now I add the column right away
  this.RadGrid1.MasterTableView.Columns.Add(boundColumn);
  boundColumn.UniqueName = "Name" + col;
  boundColumn.DataField = "Data" + col;
  boundColumn.HeaderText = "Header" + col;
 }
}

This worked (postbacks now keep the column names)?!?!?

Is this a known bug in the grid control. Even though the web page I found said to do it like this, this seems like a very weird restriction. There does not seem to be any sense to it.

3 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 03 Jul 2008, 08:09 AM
Hi Jay,

Generally the grid will save columns settings in ViewState and that is why you need to add columns on on initial load. The ViewState is managed by the object parent and in order to Save/Load/Track this properly you need to have parent. Page_Load is after LoadViewState().

Greetings,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Jay
Top achievements
Rank 1
answered on 03 Jul 2008, 05:23 PM
I'm sorry, I dont think I was very clear in my question.

I was wondering why setting the GridBoundColumn properties before calling this.RadGrid1.MasterTableView.Columns.Add(boundColumn);
 makes the column names not be saved after a postback, but setting the GridBoundColumn properties immediately AFTER calling this.RadGrid1.MasterTableView.Columns.Add(boundColumn);
 makes the column names behave correctly. Look at my code sample above for the very slight difference (BOTH are in the Page_Load). Moving the Columns.Add a few lines above setting the properties works. The first example does not work, the second one does.

Thanks
0
Vlad
Telerik team
answered on 04 Jul 2008, 05:10 AM
Hi Jay,

The tricky part is:

"The ViewState is managed by the object parent and in order to Save/Load/Track this properly you need to have parent"

Sincerely yours,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
Jay
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Jay
Top achievements
Rank 1
Share this question
or