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

[Solved] Dynamically generated grid databinding problem

2 Answers 157 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mirosław Pragłowski
Top achievements
Rank 1
Mirosław Pragłowski asked on 17 Aug 2009, 10:13 AM
Hello,

I've a problem with (as I suppose) data binding to dynamically generated grid.
Below you will find events fired when I try to edit item in grid (InPlace).
Columns in grid are dynamically generated - bacically there are 2 types of columns:
- dictionary: GridDropDownColumn (working ok)
- numeric: GridNumericColumn (cause of problems)
Grid could have multiple columns of both types depending on the underlying business object.

Problem is: when I click edit on grid item the editors are not showing.

Here is a events trace which are raised:

// loading a page 

Page_Init
GenerateDefinitionColumns
CreateColumnForVariable: Dictionary 1
CreateColumnForVariable: Numeric 1
rgDefinitionItems_NeedDataSource
rgDefinitionItems_CreateColumnEditor: Creating column editor for column: IN1
rgDefinitionItems_ItemDataBound: Binding item in view mode
rgDefinitionItems_ItemDataBound: Binding item in view mode

// here I've clicked edit button on grid

Page_Init after postback
rgDefinitionItems_CreateColumnEditor: Creating column editor for column: IN1
rgDefinitionItems_ItemCommand: Command name: Edit
rgDefinitionItems_NeedDataSource
rgDefinitionItems_ItemDataBound: Binding item in edit mode
rgDefinitionItems_CreateColumnEditor: Creating column editor for column: OUT2

In attached sample project you could methods GetCellValue & SetValueToCell 
responsible for geting and setting value into cell (in both view and edit mode).

If you remove from those methods "if (dataItem[columnUniqueName].Controls.Count > 0)"
you will see my problem - the editor control for numeric column is not created yet :(

Source code for this problem can be downloaded from dropbox:
http://dl.getdropbox.com/u/1495591/RadGridDataBindingProblem.zip

Any ideas what's wrong ?

2 Answers, 1 is accepted

Sort by
0
Mirosław Pragłowski
Top achievements
Rank 1
answered on 17 Aug 2009, 10:25 AM
More details:

If you disable ajax it is clear what is causing that editors are not shown, here is a stack trace:

 
[ArgumentOutOfRangeException: Specified argument was out of the range of valid values. 
Parameter name: index] 
   System.Web.UI.ControlCollection.get_Item(Int32 index) +8670146 
   Telerik.Web.UI.GridNumericColumnEditor.LoadControlsFromContainer() +67 
   Telerik.Web.UI.GridColumnEditorBase.InitializeFromControl(Control containerControl) +50 
   Telerik.Web.UI.GridDataItem.InitializeEditorInCell(IGridEditableColumn column) +157 
   Telerik.Web.UI.GridEditManager.GetColumnEditor(IGridEditableColumn column) +175 
   Telerik.Web.UI.GridBoundColumn.FillValues(IDictionary newValues, GridEditableItem editableItem) +81 
   Telerik.Web.UI.GridEditableItem.ExtractValues(IDictionary newValues) +175 
   Telerik.Web.UI.GridTableView.ExtractValuesFromItem(IDictionary newValues, GridEditableItem editedItem) +116 
   Telerik.Web.UI.GridItemBuilder.CreateItems(GridGroupingContext group) +1211 
   Telerik.Web.UI.GridTableView.CreateItems(IEnumerator enumerator, GridColumn[] columns, ControlCollection controls) +187 
   Telerik.Web.UI.GridTableView.CreateControlHierarchy(Boolean useDataSource) +1077 
   Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +786 
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +57 
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114 
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31 
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142 
   Telerik.Web.UI.GridTableView.PerformSelect() +28 
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73 
   Telerik.Web.UI.GridTableView.DataBind() +368 
   Telerik.Web.UI.GridTableView.Rebind() +98 
   Telerik.Web.UI.GridCommandEventArgs.ExecuteCommand(Object source) +395 
   Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +191 
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +165 
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 
   System.Web.UI.WebControls.ImageButton.OnCommand(CommandEventArgs e) +111 
   System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +176 
   System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565 

So still the question is why the editor for numeric column in not created before ItemDataBound event ? 
0
Veli
Telerik team
answered on 20 Aug 2009, 06:10 AM
Hi Mirosław,

I can see that you are creating RadGrid columns dynamically of declarative RadGrid first in Page_Init, then in Page_PreRender after postback.

To have RadGrid properly initialize its dynamic columns, you need observe a couple of requirements for dynamic initialization. Here are a few articles for more information:

Programmatic creation

Changing the grid structure dynamically on postback

In the second article, you can see our recommendation. You need to create the entire grid in Page_Init and add it to the controls collection of your form. Additionally, you need to set EnableColumnViewState="false" in the MasterTableView properties, so that the grid knows the columns collection may change.

Kind regards,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Mirosław Pragłowski
Top achievements
Rank 1
Answers by
Mirosław Pragłowski
Top achievements
Rank 1
Veli
Telerik team
Share this question
or