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

[Solved] Error Editing Row of Programmatically Populated Grid

1 Answer 161 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 20 May 2009, 03:26 PM
Hi, I'm trying to implement a grid that is programmatically populated from a DataTable by following the instructions here: http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html.

Here's the markup for the page:

<html xmlns="http://www.w3.org/1999/xhtml"
<head id="Head1" runat="server"
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"
    <div> 
        <telerik:RadCodeBlock runat="server" ID="radCodeBlockCommitmentPull"
 
            <script type="text/javascript">   
                window.onload="history.go(1);"   
                 
                var rowClicked = -1;                                                    
                 
                function RowClicked(sender, eventArgs) 
                { 
                    rowClicked = eventArgs.get_itemIndexHierarchical(); 
   
                    EditRow(rowClicked); 
                }                           
                 
                function EditRow(val)  
                { 
                    var masterTable = $find("<%= commitmentPullGrid.ClientID %>").get_masterTableView(); 
                     
                    masterTable.editItem(masterTable.get_dataItems()[rowClicked].get_element()); 
                }                
            </script> 
 
        </telerik:RadCodeBlock> 
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"
            <AjaxSettings> 
                <telerik:AjaxSetting AjaxControlID="commitmentPullGrid"
                    <UpdatedControls> 
                        <telerik:AjaxUpdatedControl ControlID="commitmentPullGrid" LoadingPanelID="LoadingPanel" /> 
                    </UpdatedControls> 
                </telerik:AjaxSetting> 
            </AjaxSettings> 
        </telerik:RadAjaxManager> 
        <telerik:RadScriptManager ID="RadScriptManagerCommitmentPull" runat="server"
        </telerik:RadScriptManager> 
        <telerik:RadAjaxLoadingPanel ID="LoadingPanel" runat="server" HorizontalAlign="Center" 
            Style="left: 50%; top: 50%" Transparency="10"
            &nbsp;<img alt="Loading..." src='<%= RadAjaxLoadingPanel.GetWebResourceUrl(Page, "Telerik.Web.UI.Skins.Default.Ajax.loading6.gif") %>' 
                style="border: 0px; vertical-align: middle;" /> 
        </telerik:RadAjaxLoadingPanel> 
        <asp:Literal ID="noResultsControl" runat="server"
        </asp:Literal> 
        <telerik:RadGrid ID="commitmentPullGrid" runat="server" AutoGenerateColumns="True" 
            GridLines="Both" Height="300px" Width="97%" Skin="Telerik" AllowSorting="True" 
            BorderWidth="0" PageSize="50" OnEditCommand="commitmentPullGrid_EditCommand"
            <FooterStyle Wrap="False" /> 
            <ItemStyle Wrap="False" /> 
            <EditItemStyle Wrap="False" /> 
            <CommandItemStyle Wrap="False" /> 
            <FilterItemStyle Wrap="False" /> 
            <HeaderStyle Wrap="False" /> 
            <MasterTableView EditMode="InPlace"
            </MasterTableView> 
            <ClientSettings EnablePostBackOnRowClick="false" EnableRowHoverStyle="True"
                <ClientEvents OnRowClick="RowClicked" /> 
                <Scrolling AllowScroll="True" UseStaticHeaders="true" /> 
            </ClientSettings> 
            <PagerStyle NextPageText="Next" PrevPageText="Prev" Mode="NextPrevAndNumeric"></PagerStyle> 
        </telerik:RadGrid> 
    </div> 
    </form> 
</body> 
</html> 

The code-behind looks like so:

    public partial class CommitmentPull : System.Web.UI.Page 
    { 
        #region page methods 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            LoadCommitments(); 
        } 
        #endregion 
        #region private methods 
        private void LoadCommitments() 
        { 
            PopulateGridWithLookupData(); 
        } 
 
        private void PopulateGridWithLookupData() 
        { 
            DataTable table; 
            DataRow dataRow; 
            string[] itemArray; 
            string[] dataKeyNames; 
            GridBoundColumn boundColumn; 
 
            table = new DataTable(); 
 
            for (int i = 0; i < 8; i++) 
            { 
                table.Columns.Add("Col" + i); 
                boundColumn = new GridBoundColumn(); 
                boundColumn.DataField = "Col" + i; 
                boundColumn.HeaderText = "Col" + i; 
                commitmentPullGrid.Columns.Add(boundColumn); 
            } 
 
            for (int i = 0; i < 5; i++) 
            { 
                dataRow = table.NewRow(); 
                itemArray = new string[8]; 
 
                for (int j = 0; j < 8; j++) 
                { 
                    itemArray[j] = (i + j) + ""
                } 
 
                dataRow.ItemArray = itemArray; 
                table.Rows.Add(dataRow); 
            } 
 
            dataKeyNames = new string[commitmentPullGrid.Columns.Count]; 
 
            for (int i = 0; i < commitmentPullGrid.Columns.Count; i++) 
            { 
                dataKeyNames[i] = commitmentPullGrid.Columns[i].UniqueName; 
            } 
 
            commitmentPullGrid.AutoGenerateColumns = false
            commitmentPullGrid.DataSource = table; 
        } 
 
        protected void commitmentPullGrid_EditCommand(object source, GridCommandEventArgs e) 
        { 
 
        } 
        #endregion 
    } 

Now, as you can probably tell when I click a row in the grid I want to perform an Edit on that grid row. This appears to begin to work, as I am able to step into the commitmentPullGrid_EditCommand() event handler, but then I get this page error:

Specified argument was out of the range of valid values.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:


[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index]
   System.Web.UI.ControlCollection.get_Item(Int32 index) +8668910
   Telerik.Web.UI.GridTextBoxColumnEditor.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) +82
   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) +1075
   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
   Telerik.Web.UI.GridItem.FireCommandEvent(String commandName, Object commandArgument) +80
   Telerik.Web.UI.RadGrid.RaisePostBackEvent(String eventArgument) +3838
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565


I cannot figure out why the edit function does not work. It works fine if I have AutoGenerateColumns set to true and bind directly to the DataTable, but once I add GridBoundColumns instead the error shows up. Any ideas as to what I might be doing wrong?

Thanks.

 





1 Answer, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 25 May 2009, 10:53 AM
Hi Andrew,

I went through your code and here are my findings:

  • Please try placing the RadScriptManager on top of all controls in the form, just after the opening <form> tag.
  • Try modifying your code for adding grid columns as below:
    if(!IsPostBack)  
    {  
        boundColumn = new GridBoundColumn();    
        commitmentPullGrid.Columns.Add(boundColumn);    
        boundColumn.DataField = "Col" + i;    
        boundColumn.HeaderText = "Col" + i;    
  • Try binding your grid through the NeedDataSource instead of using simple data-bind. More information on both binding approaches is available in the below articles:
    http://www.telerik.com/help/aspnet-ajax/grdadvanceddatabinding.html
    http://www.telerik.com/help/aspnet-ajax/grdsimpledatabinding.html

Check it out and let me know how it goes. 

All the best,
Iana
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
Andrew
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Share this question
or