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

[Solved] Problems with Programmatic Creation

2 Answers 250 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 2
Steve asked on 24 Jan 2008, 03:21 AM

I have a grid that is defined statically, with columns created dynamically.  I followed your documentation on how to do this, creating columns in Page_Load, but when I do that, the columns disappear when sorting.
The grid is ajaxed to update itself via a RadAjaxManager.  If I try to add the columns on Page_Init, i get the following javascript error:

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Cannot find column LocationName.

NOTE: "LocationName" is a valid name of a field in my datasource.

Here's the full skinny.  I have a page opened in a RadWindow.  That page loads a user control in Page_Load that has my grid.  The user control does some grid setup in Page_Init, then adds the columns in Page_Load (an example of adding a column):

Telerik.Web.UI.GridBoundColumn gridCol = new Telerik.Web.UI.GridBoundColumn();
grid.MasterTableView.Columns.Add(gridCol);
gridCol.HeaderText = "Location";
gridCol.UniqueName = "LocationName";
gridCol.DataField = "LocationName";
if (grid.AllowSorting) gridCol.SortExpression = "LocationName";
gridCol.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
gridCol.ItemStyle.Width = Unit.Percentage(25);
gridCol.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
gridCol.HeaderStyle.Width = Unit.Percentage(25);

I'm using NeedDataSource.  When I click the "LocationName" column to sort (or any column, for that matter), the entire grid goes away.  If I do the exact same column adding in Page_Init, the javascript error I posted above occurs.  Any help?

2 Answers, 1 is accepted

Sort by
0
Joe
Top achievements
Rank 1
answered on 25 Jan 2008, 05:12 PM

I am having the same problem. On Sort or Page, my entire grid goes away. On my page, the user uploads a delimited file. OnClick, a dataSet is populated by the delimited file, I assign the dataset as datasource, add the column and rebind.

//////////////////////////////////////////////////////////////////////////////////////


this

.RadGrid1.DataSource = dataSet;
this.RadGrid1.DataBind();

// the following adds a column to my empty grid which does specify
// everything necessary except for the column specs. See the HTML below.

GridBoundColumn col1 = new GridBoundColumn();
RadGrid1.Columns.Add(col1);col1.DataField = "id";
col1.UniqueName =
"id";
col1.AllowSorting =
true;
col1.HeaderText =
"Certificate #";
if (RadGrid1.AllowSorting) col1.SortExpression = "id Asc";
col1.DataFormatString =
"{0,-10:D6}"; // be sure to pad with zeros!

RadGrid1.Rebind();

//////////////////////////////////////////////////////////////////////////////////////

<
rad:RadGrid ID="RadGrid1" ClientSettings-ReorderColumnsOnClient="true" runat="server" AllowPaging="True" AllowSorting="True" EnableAJAX="True" EnableViewState="false" GridLines="None" Height="300px">
  <
MasterTableView DataKeyNames="id" AllowSorting="true" runat="server">
     <SortExpressions>
             <rad:GridSortExpression FieldName="id" SortOrder="Ascending"/>
     </SortExpressions>
   <
RowIndicatorColumn Visible="False">
             <HeaderStyle Width="20px" />
   </RowIndicatorColumn>
   <ExpandCollapseColumn Resizable="False" Visible="False">
             <HeaderStyle Width="20px" />
   </ExpandCollapseColumn>
</MasterTableView>
</rad:RadGrid> 

//////////////////////////////////////////////////////////////////////////////////////

0
Joe
Top achievements
Rank 1
answered on 25 Jan 2008, 08:27 PM
I resolved the problem. The code for populating/repopulating the grid MUST be referenced on the Page_Load event.

In my case, since I am populating based upon the upload of a data file which is then stored in a cookie as a datatable. Now, on Page_Load, I check the cookie to make sure that a valid datatable is there, and if it is not null, I rebind the datatable to the grid.



Tags
Grid
Asked by
Steve
Top achievements
Rank 2
Answers by
Joe
Top achievements
Rank 1
Share this question
or