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

RadGrid - PerformInsert - "Insert item is available only when grid is in insert mode."

3 Answers 179 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sven
Top achievements
Rank 1
Sven asked on 18 Oct 2013, 07:30 PM
I'm getting "Insert item is available only when grid is in insert mode." when I call GetInsertItem() for the PerformInsert command in the ItemCommand event.  Is there something obvious I'm missing?  Thanks.

<telerik:RadGrid ID="grid" runat="server"
                OnNeedDataSource="grid_NeedDataSource"
                OnItemCommand="grid_ItemCommand"               
                OnInsertCommand="grid_InsertCommand">               
                <MasterTableView CommandItemDisplay="Top" ClientDataKeyNames="id" DataKeyNames="id" AutoGenerateColumns="false" EditMode="EditForms">                  
                    <CommandItemTemplate>
                      <asp:LinkButton ID="add" runat="server" Text="Add new Record" CommandName="InitInsert"></asp:LinkButton>
                      <asp:LinkButton ID="btnPerformInsert" runat="server" Text="Add new Record" CommandName="PerformInsert" Visible="true"></asp:LinkButton>
                    </CommandItemTemplate>                   
                    <Columns>                                              
                        <telerik:GridEditCommandColumn ButtonType="ImageButton" HeaderStyle-Width="10%"  />                                               
                        <telerik:GridButtonColumn CommandName="Delete" UniqueName="DeleteColumn" ButtonType="ImageButton" HeaderStyle-Width="10%" />...                       
                                   
                    </Columns>
                    <EditFormSettings EditFormType="Template">
                        <FormTemplate>                           
                                ....
                                    <asp:Button ID="btnUpdate" Text="Update" runat="server" CommandName="Update" Visible='<%# !(Container.DataItem is Telerik.Web.UI.GridInsertionObject) %>' />
                                    <asp:Button ID="btnInsert" Text="Insert" runat="server" CommandName="PerformInsert" Visible='<%# Container.DataItem is Telerik.Web.UI.GridInsertionObject %>'  />
                                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel" />
                                </div>
                            </div>
                        </FormTemplate>
                    </EditFormSettings>

protected void grid_ItemCommand(object sender, GridCommandEventArgs e)
       {          
           switch(e.CommandName)
           {   
               case RadGrid.PerformInsertCommandName:                   
    
               
GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item.OwnerTableView.GetInsertItem();

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 19 Oct 2013, 05:33 AM
Hello,

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
   {
       if (e.CommandName == RadGrid.PerformInsertCommandName)
       {
           GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
           //perform your logic here
       }
   }
   //OR
   protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
   {
       GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
       //perform your logic here
   }


Thanks,
Jayesh Goyani
0
Sven
Top achievements
Rank 1
answered on 19 Oct 2013, 03:55 PM
Jayesh, thanks for the quick reply.  I tried both of your suggestions, but neither worked for me.  In both cases e.Item was of type GridCommandItem (not GridEditFormInsertItem), so the cast was null.
0
Princy
Top achievements
Rank 1
answered on 21 Oct 2013, 04:31 AM
Hi Sven,

Please modify the code as below.Remove the LinkButton with id=btnPerformInsert.

ASPX:
<CommandItemTemplate>
 <asp:LinkButton ID="add" runat="server" Text="Add new Record" CommandName="InitInsert"></asp:LinkButton>
</CommandItemTemplate>

C#:
protected void grid_InsertCommand(object sender, GridCommandEventArgs e)
   {
     GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
     //Code to Insert
   }

Thanks,
Princy
Tags
Grid
Asked by
Sven
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Sven
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Share this question
or