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

Insert item is available onlly when grid is in insert mode. Error

2 Answers 190 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pete
Top achievements
Rank 1
Pete asked on 14 May 2010, 04:09 PM
Hello,
  • .Net 3.5Sp1
  • Win2003 server
  • IE 7 and 8
  • Telerik RadControls 2010.1.45  (2010 NET35)
  • preferred programming language C#
  • Receiving the error: "Insert Item is available only when grid is in insert mode."  Shown at the end of this post.  I have a grid with 3 text boxes and 3 dropdown lists.  When I click a link " Add new item" it calls the RadGrid1_InsertCommand which would add a blank record to my data source and then I rebind.  I do not modify any of the controls directly just the data source and rebind.  Suddenly I have started to recieve this error and I don't understand what has changed for me to start getting this error. I've had this working fine.  Please provide any insight you may have.

    <Page snippet>
     <rad:RadGrid ID="RadGrid1"
                            runat="server"
                            AutoGenerateColumns="False"
                            BorderStyle="Groove"
                            BorderWidth="2px"                       
                            UseAccessibleHeader="true"                       
                            Width="95%"
                            EnableEmbeddedSkins="false"
                            Skin="Skin1"
                            OnPreRender="RadGrid1_PreRender" GridLines="None" >
                            <itemstyle backcolor="Gainsboro" />
                            <alternatingitemstyle backcolor="WhiteSmoke" />
                            <MasterTableView EditMode="InPlace" DataKeyNames="EKey"
                                CommandItemDisplay="Bottom" AddNewRecordText="New Item" horizontalalign="Center">
                                <CommandItemTemplate>
                                    <asp:LinkButton ID="lnkAddNew" runat="server" CommandName="PerformInsert" ><img style="border:0px;" alt="New Item" src="/Grid/AddRecord.gif" /> Add CPE</asp:LinkButton>
                                </CommandItemTemplate>
                                <rowindicatorcolumn>
                                    <HeaderStyle Width="20px" />
                                </rowindicatorcolumn>
                                <expandcollapsecolumn>
                                    <HeaderStyle Width="20px" />
                                </expandcollapsecolumn>
                                <Columns>                                                       
                               
                                <rad:GridBoundColumn DataField="EKey" ReadOnly="false" Visible="false" UniqueName="ElementCPEKey"/>
                                <%--<rad:GridBoundColumn DataField="CKey" ReadOnly="false" Visible="false" UniqueName="CPEKey"/>--%>                                                                                 
                                                           
                                <rad:GridTemplateColumn HeaderText="Item Name" ItemStyle-Wrap="true" ItemStyle-width="20%" ItemStyle-CssClass="StdFont">
                                    <ItemTemplate><%# Eval("ItemNameText")%></ItemTemplate>                                                  
                                    <EditItemTemplate >
                                        <rad:RadTextBox ID="txtItemName" runat="server" Columns="60" Text='<%# Eval("ItemName") %>' width="99%"  OnTextChanged="txtItemName_OnTextChanged" AutoPostBack="true"></rad:RadTextBox>
                                        <br />                                                              
                                    </EditItemTemplate>                            
                                    <ItemStyle Width="20%" Wrap="True" />
                                </rad:GridTemplateColumn>
    </End Page Snippet>
    <Code Behind Grid Events>
     #region Grid methods
        private void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode)
            {
                //LoadLookupData(RadGrid1.MasterTableView.Items.Count - 1);
                GridEditableItem editedItem = e.Item as GridEditableItem;
                EDTO gridDTO = e.Item.DataItem as EDTO;

                VMSDropDownList ddl = editedItem.FindControl("ddlList") as VMSDropDownList;
                ddl.DataTextField = "Descrip";
                ddl.DataValueField = "ListKey";
                ddl.DataSource = Manager.RetrieveList(session);
                ddl.DataBind();
                ddl.Items.Insert(0, new ListItem("None"));
                ddl.SelectedValue = 1;

                <... repeated twice with two other lists ...>
                }
        }
        void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
        {
            //if (dsDTO == null)
            //{
            //    dsDTO = new DataSource<EDTO>();
            //    AddBlankRow();
            //}
            //else
            //{
            //    GridDataItem dataItem = RadGrid1.MasterTableView.Items[RadGrid1.MasterTableView.Items.Count - 1];

            //    RadTextBox itemTextBox = dataItem.FindControl("txtItemName") as RadTextBox;
            //    RadTextBox itemIDTextBox = dataItem.FindControl("txtItemID") as RadTextBox;
            //    if (itemTextBox.Text.Length > 0 && itemIDTextBox.Text.Length > 0)
            //    {
            //        //dataItem.Enabled = false;
            //        AddBlankRow();
            //    }
            //    else
            //    {
            //        SetResultText("An item must be selected before adding a new record to this element.");
            //        BindElementGrid();
            //    }
            //}
        }
         void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
        {
            if (dsDTO != null)
            {
                if (dsDTO.DataItems[e.Item.ItemIndex].IsLoadedFlag == 1) //we loaded from database
                    dsDTO.DataItems[e.Item.ItemIndex].DeleteFlag = 1;
                else
                    dsDTO.DataItems.RemoveAt(e.Item.ItemIndex); //This is new, the user just added it so remove it from the list.
                if (e.Item.ItemIndex == 0)
                    dsDTO = null;

                BindElementGrid();
            }
        }
       private void BindElementGrid()
        {
            if (dsDTO != null)
            {
                RadGrid1.DataSource = dsDTO;
                //RadGrid1.DataSource = RetrieveActiveDataSource();
                for (int i = 0; i < RadGrid1.PageSize; i++)
                {
                    RadGrid1.EditIndexes.Add(i);
                }
                this.RadGrid1.DataBind();

                ViewState[this.UniqueID + "dsDTO"] = dsDTO;
            }
        }

    //Tried to use this instead to get rid of the GetInsert() error
        private void RadGrid_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "PerformInsert")
            {
                if (dsDTO == null)
                {
                    dsDTO = new DataSource<EDTO>();
                    AddBlankRow();
                }
                else
                {
                    GridDataItem dataItem = RadGrid1.MasterTableView.Items[RadGrid1.MasterTableView.Items.Count - 1];

                    
                    RadTextBox itemTextBox = dataItem.FindControl("txtItemName") as RadTextBox;
                    RadTextBox itemIDTextBox = dataItem.FindControl("txtItemID") as RadTextBox;
                    if (itemTextBox.Text.Length > 0 && itemIDTextBox.Text.Length > 0)
                    {
                        //dataItem.Enabled = false;
                        AddBlankRow();
                    }
                    else
                    {
                        SetResultText("An item must be selected before adding a new record to this element.");
                        BindElementCPEGrid();
                    }
                }
            }
        }
    #endregion
    </End Code Behind Grid Events>
    Server Error in '/' Application.
    --------------------------------------------------------------------------------

    Insert item is available only when grid is in insert mode.
    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: Telerik.Web.UI.GridException: Insert item is available only when grid is in insert mode.

    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:

    [GridException: Insert item is available only when grid is in insert mode.]
       Telerik.Web.UI.GridTableView.GetInsertItem() +211
       Telerik.Web.UI.GridCommandEventArgs.ExecuteCommand(Object source) +2910
       Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +134
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
       Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +38
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
       Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +115
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
       System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +118
       System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +135
       System.Web.UI.WebControls.LinkButton.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) +175
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

     

    2 Answers, 1 is accepted

    Sort by
    0
    Pavlina
    Telerik team
    answered on 14 May 2010, 04:27 PM
    Hi Pete ,

    I suggest you examine the forum thread below and let me know if it helps to resolve the issue you are facing:
    http://www.telerik.com/community/forums/aspnet-ajax/grid/insert-item-is-available-only-when-grid-is-in-insert-mode.aspx

    Kind regards,
    Pavlina
    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
    Pete
    Top achievements
    Rank 1
    answered on 14 May 2010, 04:51 PM

    Thanks Pavlina,

    Setting e.Canceled = true  right before the bind did the trick.  I missed that post in my search. Not sure why all of a sudden I have to do this, but thanks, your help is appreciated.

    Pete

    Tags
    Grid
    Asked by
    Pete
    Top achievements
    Rank 1
    Answers by
    Pavlina
    Telerik team
    Pete
    Top achievements
    Rank 1
    Share this question
    or