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

[Solved] Unable to cast object of type Telerik.Web.UI.GridCommandItem to type 'Telerik.Web.UI.GridEditableItem

1 Answer 268 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bruce Lam
Top achievements
Rank 1
Bruce Lam asked on 19 Oct 2009, 09:42 AM
I create
 <CommandItemTemplate>
                                         <table width="100%">
                                          <tr>
                                            <td>
                                             <asp:LinkButton ID="btnAdd"
                                              Text="Add New Content of Course" CommandName="InitInsert" Runat="server"></asp:LinkButton>
                                            </td>
                                            <td>
                                              <asp:LinkButton ID="btnDelete"
                                              Text="Delete Content" CommandName="Delete" Runat="server"></asp:LinkButton>
                                            </td>
                                        <td>
                                         <asp:LinkButton ID="btnRefresh" Text="Refresh data"
                                          CommandName="Rebind" Runat="server"></asp:LinkButton>
                                        </td>
                                          </tr>
                                         </table>
</CommandItemTemplate>

and When I delete item i show error Unable to cast object of type Telerik.Web.UI.GridCommandItem to type 'Telerik.Web.UI.GridEditableItem . I dont know what happen. This is Code i delete item in event delete command

protected void rgTrCategory_DeleteCommand(object source, GridCommandEventArgs e)
    {
        bool result = false;
        try
        {
            if (e.CommandName == RadGrid.DeleteCommandName)
            {
                if ("TrContents".Equals(e.Item.OwnerTableView.Name))
                {
                    for (int i = 0; i < e.Item.OwnerTableView.Items.Count; i++)
                    {
                        GridDataItem row = e.Item.OwnerTableView.Items[i];
                        if (row.Selected == true)
                        {

                            Image _imgContent = (Image)row.FindControl("imgContent");
                            if (_imgContent != null)
                            {
                                if (File.Exists(Server.MapPath(_imgContent.ImageUrl.Trim())))
                                {
                                    File.Delete(Server.MapPath(_imgContent.ImageUrl.Trim()));
                                }
                                TrAccess.DeleteTrainingContent(row.GetDataKeyValue("f_ConCode").ToString());
                                result = true;
                            }
                        }
                    }
                    if (result == true)
                    {
                        ShowErrorFunction.ShowError(this.Page, "Delete Successul!");
                    }
                    else
                    {
                        ShowErrorFunction.ShowError(this.Page, "Please select row to delete");
                    }
                }
            }
        }
        catch
        {


        }

    }
Please help me to solve this problem as impossible as. Thanks alot.

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 19 Oct 2009, 10:58 AM
Hello lam,

Can you try replacing the for loop in your code with a foreach loop as shown below and check if it does any help:
c#:
protected void rgTrCategory_DeleteCommand(object source, GridCommandEventArgs e) 
    { 
        bool result = false
        try 
        {            
               if ("Detail".Equals(e.Item.OwnerTableView.Name)) 
                { 
                    foreach (GridDataItem row in e.Item.OwnerTableView.Items) 
                    {                         
                        if (row.Selected == true
                        { 
 
                            Image _imgContent = (Image)row.FindControl("imgContent"); 
                            if (_imgContent != null
                            { 
                                if (File.Exists(Server.MapPath(_imgContent.ImageUrl.Trim()))) 
                                { 
                                    File.Delete(Server.MapPath(_imgContent.ImageUrl.Trim())); 
                                } 
                                TrAccess.DeleteTrainingContent(row.GetDataKeyValue("f_ConCode").ToString());                                
                                result = true
                            } 
                        } 
                    } 
                    if (result == true
                    { 
                        ShowErrorFunction.ShowError(this.Page, "Delete Successul!"); 
                    } 
                    else 
                    { 
                        ShowErrorFunction.ShowError(this.Page, "Please select row to delete"); 
                    } 
                }             
        } 
        catch 
        { 
 
        } 
    } 

Thanks
Shinu.
Tags
Grid
Asked by
Bruce Lam
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or