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

Insert records in radgrid

3 Answers 208 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Parodist
Top achievements
Rank 1
Parodist asked on 20 Jan 2009, 05:10 PM

Unable to insert records in the row :
Error message - {"Unable to cast object of type 'Telerik.Web.UI.GridCommandItem' to type 'Telerik.Web.UI.GridEditFormInsertItem'."}
Am I missing something ??

protected void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
    {  
         
        GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;  
        string Activity = (insertedItem["Activity"].Controls[0] as TextBox).Text;  
        string Status = (insertedItem["Status"].Controls[0] as TextBox).Text;  
        string Comments = (insertedItem["Comments"].Controls[0] as TextBox).Text;  
 
        try 
        {  
            string Query = null;  
 
            Query = "INSERT into  SheduleActivityDetails(Activity,Status,Comments) values('" + Activity + "','" + Status + "','" + Comments + "')";  
            objDataLayer.ExecuteNonQuery(Query);  
            RebindRadGrid();  
        }  
        catch (Exception ex)  
        {  
            RadGrid1.Controls.Add(new LiteralControl("Unable to insert Activity. Reason: " + ex.Message));  
            e.Canceled = true;  
        }  
 




 

<telerik:RadGrid ID="RadGrid1" runat="server"    
        Width="600px" AllowMultiRowSelection="false" AllowMultiRowEdit="false" AllowAutomaticInserts="false" 
        AllowAutomaticUpdates="false" AllowPaging="true" ShowStatusBar="true" AutoGenerateEditColumn="false"   
        EnableAJAX="true" OnNeedDataSource="RadGrid1_NeedDataSource"    
        OnInsertCommand="RadGrid1_InsertCommand" OnUpdateCommand="RadGrid1_UpdateCommand"   
        ShowFooter="True" OnItemCreated="RadGrid1_ItemCreated" Skin="Office2007">                                                                                                             
                                                                            
        <ClientSettings> 
            <Selecting AllowRowSelect="true" EnableDragToSelectRows="true" /> 
        </ClientSettings>                                                                          
             
          <MasterTableView CommandItemDisplay="Top" DataKeyNames="Id" EditMode="InPlace"   AllowAutomaticInserts="False"  AutoGenerateColumns="false" Width="100%">  
              
            <Columns> 
                <telerik:GridClientSelectColumn HeaderStyle-Width="40px" /> 
                  
                  
                     <telerik:GridBoundColumn DataField="Id" Visible="true"  HeaderText="Id"   
                     UniqueName="Id" ReadOnly="True">                                                                                 
                     </telerik:GridBoundColumn> 
                <%--<telerik:GridBoundColumn DataField="Activity Id" HeaderText="ID" ReadOnly="True" 
                    SortExpression="ActivityId" UniqueName="Activity ID" Visible="false">  
                    <HeaderStyle Width="20px" ForeColor="Silver" /> 
                    <ItemStyle ForeColor="Silver" /> 
                </telerik:GridBoundColumn>--%> 
                  
                  
                <telerik:GridBoundColumn DataField="Activity" HeaderText="Activity" SortExpression="Activity" 
                    UniqueName="Activity">  
                                                                     
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="Status" HeaderText="Status" SortExpression="Status" 
                    UniqueName="Status" Visible="true" EditFormColumnIndex="1">  
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="Comments" HeaderText="Comments" SortExpression="Comments" 
                    UniqueName="Comments" Visible="true" EditFormColumnIndex="1">  
                </telerik:GridBoundColumn> 
                  
                  
            </Columns> 
              
             <CommandItemTemplate> 
              
                <div style="padding: 10px 0px;">  
                      
                      
                    <asp:LinkButton Style="vertical-align: bottom" ID="btnEditSelected" runat="server" 
                        CommandName="EditSelected" CausesValidation="false" Visible='<%# RadGrid1.EditIndexes.Count == 0 %>'><img style="border:0px;vertical-align:middle;" alt="" src="=../../Images/Edit.gif" /> Edit Selected Activity</asp:LinkButton> 
                      
                    <asp:LinkButton ID="LinkButton1" runat="server" CommandName="UpdateEdited"  Visible='<%# RadGrid1.EditIndexes.Count > 0 %>'><img style="border:0px;vertical-align:middle;" alt="" src="=../../Images/Update.gif" /> Update Activity</asp:LinkButton> 
                                                                       
       
 <asp:LinkButton ID="LinkButton2" runat="server" CommandName="CancelAll" CausesValidation="false"    
                        Visible='<%# RadGrid1.EditIndexes.Count > 0 || RadGrid1.MasterTableView.IsItemInserted %>'><img style="border:0px;vertical-align:middle;" alt="" src="=../../Images/edit.png" /> Cancel editing</asp:LinkButton> 
 
 <asp:LinkButton ID="LinkButton3" runat="server" CommandName="InitInsert" Visible='<%# !RadGrid1.MasterTableView.IsItemInserted %>'><img style="border:0px;vertical-align:middle;" alt="" src="=../../Images/AddRecord.gif" /> Add new Activity</asp:LinkButton> 
                      
 <asp:LinkButton ID="LinkButton4" runat="server" CommandName="PerformInsert"   Visible='<%# RadGrid1.MasterTableView.IsItemInserted %>'><img style="border:0px;vertical-align:middle;" alt="" src="=../../Images/Insert.gif" /> Add this Activity</asp:LinkButton> 
                    &nbsp; &nbsp;&nbsp;&nbsp;  
                    
                    <asp:LinkButton ID="LinkButton5" runat="server" CommandName="RebindGrid"><img style="border:0px;vertical-align:middle;" alt="" src="=../../Images/Refresh.gif"/> Refresh Activities</asp:LinkButton> 
                </div> 
 
            </CommandItemTemplate> 
 
            <EditFormSettings  ColumnNumber="2" CaptionFormatString="Edit properties of Activity with Id {0}" 
            CaptionDataField="ActivityId">  
              
                <FormTableItemStyle Wrap="False"></FormTableItemStyle> 
                <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle> 
                <FormMainTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="5" BackColor="White" 
                    Width="100%"/>  
                <FormTableStyle CellSpacing="0" CellPadding="2" CssClass="module" Height="110px" 
                    BackColor="White"/>  
                <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle> 
                <EditColumn ButtonType="ImageButton" InsertText="Insert Order" UpdateText="Update record" 
                    UniqueName="EditCommandColumn1" CancelText="Cancel edit">  
                </EditColumn> 
                <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle> 
            </EditFormSettings> 
        </MasterTableView> 
 
    </telerik:RadGrid> 

3 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 20 Jan 2009, 05:42 PM
Hello Parodist,

I failed to reproduce this error. Please find my test project attached to this post. It would be helpful if you let me know how to modify the code in order to recreate the mentioned behavior.

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Parodist
Top achievements
Rank 1
answered on 21 Jan 2009, 03:46 PM
Thanks for your response Daniel, I have added my codes and its working but how can I hide "Edit properties of Activity with Id 1" while updating it.
 <EditFormSettings ColumnNumber="2" CaptionFormatString="Edit properties of Activity with Id {0}" 
                        CaptionDataField="Id"  > 

Thanking you in advance.
<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Office2007" ShowFooter="True" 
                OnNeedDataSource="RadGrid1_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand" AutoGenerateColumns="False" OnInsertCommand="RadGrid1_InsertCommand" 
                OnItemInserted="RadGrid1_ItemInserted" GridLines="None">  
                <ClientSettings> 
                    <Selecting AllowRowSelect="true" EnableDragToSelectRows="true" /> 
                </ClientSettings> 
                <%--<MasterTableView CommandItemDisplay="Top" DataKeyNames="Id" EditMode="InPlace" Width="100%">--%> 
                <MasterTableView CommandItemDisplay="Top" DataKeyNames="Id" HorizontalAlign="Center">  
                    <CommandItemTemplate> 
                      
                      
                        <%--<asp:LinkButton ID="LinkButton3" runat="server" CommandName="InitInsert" Visible='<%# !RadGrid1.MasterTableView.IsItemInserted %>'> Add new Activity</asp:LinkButton>--%> 
                        <asp:LinkButton ID="LinkButton3" runat="server" CommandName="InitInsert" Visible='<%# !RadGrid1.MasterTableView.IsItemInserted %>'><img style="border:0px;vertical-align:middle;" alt="" src="=../../Images/AddRecord.gif" /> Add new Activity</asp:LinkButton> 
                        <asp:LinkButton Style="vertical-align: bottom" ID="btnEditSelected" runat="server" 
                            CommandName="EditSelected" CausesValidation="false" Visible='<%# RadGrid1.EditIndexes.Count == 0 %>'><img style="border:0px;vertical-align:middle;" alt="" src="=../../Images/Edit.gif" /> Edit Selected Activity</asp:LinkButton> 
                          <%--<asp:LinkButton ID="LinkButton1" runat="server" CommandName="UpdateEdited" Visible='<%# RadGrid1.EditIndexes.Count > 0 %>'><img style="border:0px;vertical-align:middle;" alt="" src="=../../Images/Update.gif" /> Update Activity</asp:LinkButton>  --%> 
                         <asp:LinkButton ID="LinkButton5" runat="server" CommandName="RebindGrid"><img style="border:0px;vertical-align:middle;" alt="" src="=../../Images/Refresh.gif"/> Refresh Activities</asp:LinkButton>     
                              
                    </CommandItemTemplate> 
                    <RowIndicatorColumn Visible="false">  
                        <HeaderStyle Width="20px"></HeaderStyle> 
                    </RowIndicatorColumn> 
                    <ExpandCollapseColumn Visible="False" Resizable="False">  
                        <HeaderStyle Width="20px"></HeaderStyle> 
                    </ExpandCollapseColumn> 
                    <Columns> 
                        <telerik:GridClientSelectColumn HeaderStyle-Width="40px">  
                            <HeaderStyle Width="40px"></HeaderStyle> 
                        </telerik:GridClientSelectColumn> 
                        <telerik:GridBoundColumn DataField="Id" Visible="false" HeaderText="Id" UniqueName="Id" 
                            ReadOnly="True">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="Activity" HeaderText="Activity" SortExpression="Activity" 
                            UniqueName="Activity">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="Status" HeaderText="Status" SortExpression="Status" 
                            UniqueName="Status" Visible="true" EditFormColumnIndex="1">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="Comments" HeaderText="Comments" SortExpression="Comments" 
                            UniqueName="Comments" Visible="true" EditFormColumnIndex="1">  
                        </telerik:GridBoundColumn> 
                    </Columns> 
                    <EditFormSettings ColumnNumber="2" CaptionFormatString="Edit properties of Activity with Id {0}" 
                        CaptionDataField="Id"  > 
                        <FormTableItemStyle Wrap="true"></FormTableItemStyle> 
                        <%--<FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>--%> 
                        <FormMainTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="0" BackColor="White" 
                            Width="10%" /> 
                        <FormTableStyle CellSpacing="5" CellPadding="5" CssClass="module" Height="2px" BackColor="White" /> 
                        <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle> 
                        <EditColumn ButtonType="ImageButton" InsertText="Insert Order" UpdateText="Update record" 
                            UniqueName="EditCommandColumn1" CancelText="Cancel edit">  
                        </EditColumn> 
                        <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle> 
                        <PopUpSettings ScrollBars="None"></PopUpSettings> 
                    </EditFormSettings> 
                </MasterTableView> 
            </telerik:RadGrid> 
 protected void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
    {  
        GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;  
 
        string Activity = (insertedItem["Activity"].Controls[0] as TextBox).Text;  
        string Status = (insertedItem["Status"].Controls[0] as TextBox).Text;  
        string Comments = (insertedItem["Comments"].Controls[0] as TextBox).Text;  
 
        try 
        {  
            string Query = null;  
 
            Query = "INSERT into ScheduleActivityDetails(ActivityId,Activity,Status,Comments) values('" + RadComboBox1.SelectedValue + "','" + Activity + "','" + Status + "','" + Comments + "') ";  
            objDataLayer.ExecuteNonQuery(Query);  
            RebindRadGrid();  
 
        }  
        catch (Exception ex)  
        {  
            RadGrid1.Controls.Add(new LiteralControl("Unable to insert Activity. Reason: " + ex.Message));  
            e.Canceled = true;  
        }  
 
 
 
 
        //RadAjaxManager1.Alert(insertedItem.ItemType.ToString());  
    }  
 
 
    protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
    {  
        //Get the GridEditableItem of the RadGrid       
        GridEditableItem editedItem = e.Item as GridEditableItem;  
        //Get the primary key value using the DataKeyValue.       
        string Id = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["Id"].ToString();  
        //Access the textbox from the edit form template and store the values in string variables.       
        string Activity = (editedItem["Activity"].Controls[0] as TextBox).Text;  
        string Status = (editedItem["Status"].Controls[0] as TextBox).Text;  
        string Comments = (editedItem["Comments"].Controls[0] as TextBox).Text;  
 
        try 
        {  
            string Query = null;  
            Query = "UPDATE ScheduleActivityDetails set Activity='" + Activity + "',Status='" + Status + "',Comments='" + Comments + "' where Id='" + Id + "'";  
            objDataLayer.ExecuteNonQuery(Query);  
            // RadGrid1.Rebind();  
            RebindRadGrid();  
        }  
        catch (Exception ex)  
        {  
            RadGrid1.Controls.Add(new LiteralControl("Unable to update Activity. Reason: " + ex.Message));  
            e.Canceled = true;  
        }  
    }  
 
0
Daniel
Telerik team
answered on 26 Jan 2009, 02:00 PM
Hello Parodist,

You can hide the caption of your edit form in Insert mode as shown below:
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    if (e.CommandName == "InitInsert") 
        RadGrid1.MasterTableView.EditFormSettings.CaptionFormatString = ""

Let me know if I'm missing something.

Kind regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Parodist
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Parodist
Top achievements
Rank 1
Share this question
or