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