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

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

4 Answers 431 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ian lipsky
Top achievements
Rank 1
ian lipsky asked on 08 Aug 2013, 10:31 PM
http://www.telerik.com/help/aspnet-ajax/grid-performing-batch-updates.html

I'm trying to use the example in that demo to allow someone to edit multiples rows in a grid and then submit the changes all at once. However, I keep getting the following error:

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


I'm not sure what's wrong here. In my Radgrid1_ItemCommand function I've left out ALL of the code - it's just an empty function that does nothing. and i still get the above error. Any suggestions?

ASPX
<telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
        <telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0" DataSourceID="SqlDataSource1" GridLines="None" AutoGenerateEditColumn="True" AllowMultiRowEdit="True" Width="40%" OnPreRender="RadGrid1_PreRender" AllowAutomaticUpdates="True" OnItemCommand="RadGrid1_ItemCommand" >
<MasterTableView AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1" EditMode="InPlace" CommandItemDisplay="Top">
 
    <CommandItemTemplate>
        <asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update" Visible='true' Text="Update" Font-Bold="True" Font-Size="Larger" />
    </CommandItemTemplate>
 
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
 
<ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column" Created="True">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
 
    <Columns>
        <telerik:GridBoundColumn DataField="ID" DataType="System.Int32" FilterControlAltText="Filter ID column" HeaderText="ID" ReadOnly="True" SortExpression="ID" UniqueName="ID">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Name" FilterControlAltText="Filter Name column" HeaderText="Name" SortExpression="Name" UniqueName="Name">
        </telerik:GridBoundColumn>
        <telerik:GridCheckBoxColumn DataField="IsParent" DataType="System.Boolean" FilterControlAltText="Filter IsParent column" HeaderText="IsParent" SortExpression="IsParent" UniqueName="IsParent">
        </telerik:GridCheckBoxColumn>
    </Columns>
 
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
</EditFormSettings>
 
<PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
</MasterTableView>
 
<PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
 
<FilterMenu EnableImageSprites="False"></FilterMenu>
        </telerik:RadGrid>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MachineSalesConnectionString1 %>" SelectCommand="SELECT [ID], [Name], [IsParent] FROM [Manufacturer]" UpdateCommand="UPDATE [Manufacturer] SET [Name] = @Name, [IsParent] = @IsParent WHERE [ID] = @ID">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="IsParent" Type="Boolean" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="IsParent" Type="Boolean" />
                <asp:Parameter Name="ID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>

CS
using System;
using Telerik.Web.UI;
 
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void RadGrid1_PreRender(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            foreach (GridItem item in RadGrid1.MasterTableView.Items)
            {
                if (item is GridEditableItem)
                {
                    GridEditableItem editableItem = item as GridDataItem;
                    editableItem.Edit = true;
                }
            }
            RadGrid1.Rebind();
        }
    }
 
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
    //    if (e.CommandName == "UpdateAll")
    //    {
    //        foreach (GridEditableItem editedItem in RadGrid1.EditItems)
    //        {
    //            Hashtable newnewValues = new Hashtable();
    //            //The GridTableView will fill the values from all editable columns in the hash 
    //            e.Item.OwnerTableView.ExtractValuesFromItem(newnewValues, editedItem);
    //            SqlDataSource1.UpdateCommand = String.Format("Update Manufacturers SET IsParent={0} WHERE ID={1}", newnewValues["IsParent"], editedItem["ID"].Text);
    //            SqlDataSource1.Update();
    //            editedItem.Edit = false;
    //        }
    //    }
    //    RadGrid1.Rebind();
    }  
}

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 09 Aug 2013, 03:29 AM
Hi ,

I tried your same code and it's working fine at my end.Can you please keep breakpoints and check ,on which line exactly is the error being raised.Is this your entire code or are you using anymore events? Please let me know.

Thanks,
Princy
0
ian lipsky
Top achievements
Rank 1
answered on 09 Aug 2013, 04:15 AM
I deleted the code in itemcommand - as you can see from the code i posted, i commented out all the code in there. And it still blows up. The error was occurring within the grid itself. I'm not in front of that computer right now but when the error occurred within a file something like Gridview.cs - dont remember what line.
0
Princy
Top achievements
Rank 2
answered on 09 Aug 2013, 06:12 AM
Hi Ian,

I couldn't replicate the issue,here is the full code i have tried,try creating a new RadGrid and check once more.

ASPX:
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
      </telerik:RadScriptManager>
      <telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0" DataSourceID="SqlDataSource1"
          GridLines="None" AutoGenerateEditColumn="True" AllowMultiRowEdit="True" Width="40%"
          AllowPaging="true" OnPreRender="RadGrid1_PreRender" AllowAutomaticUpdates="True"
          OnItemCommand="RadGrid1_ItemCommand">
          <MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID" DataSourceID="SqlDataSource1"
              EditMode="InPlace" CommandItemDisplay="Top">
              <CommandItemTemplate>
                  <asp:LinkButton ID="btnUpdate" runat="server" CommandName="UpdateAll" Visible='true'
                      Text="Update" Font-Bold="True" Font-Size="Larger" />
              </CommandItemTemplate>
              <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                  <HeaderStyle Width="20px"></HeaderStyle>
              </RowIndicatorColumn>
              <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column"
                  Created="True">
                  <HeaderStyle Width="20px"></HeaderStyle>
              </ExpandCollapseColumn>
              <Columns>
                  <telerik:GridBoundColumn DataField="OrderID" DataType="System.Int32" FilterControlAltText="Filter OrderID column"
                      HeaderText="OrderID" ReadOnly="True" SortExpression="OrderID" UniqueName="ID">
                  </telerik:GridBoundColumn>
                  <telerik:GridBoundColumn DataField="ShipCity" FilterControlAltText="Filter ShipCity column"
                      HeaderText="ShipCity" SortExpression="ShipCity" UniqueName="ShipCity">
                  </telerik:GridBoundColumn>
                  <telerik:GridCheckBoxColumn DataField="IsTrue" DataType="System.Boolean" FilterControlAltText="Filter IsTrue column"
                      HeaderText="IsTrue" SortExpression="IsTrue" UniqueName="IsTrue">
                  </telerik:GridCheckBoxColumn>
              </Columns>
              <EditFormSettings>
                  <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                  </EditColumn>
              </EditFormSettings>
              <PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
          </MasterTableView>
          <PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
          <FilterMenu EnableImageSprites="False">
          </FilterMenu>
      </telerik:RadGrid>

C#:
protected void RadGrid1_PreRender(object sender, System.EventArgs e)
   {
       if (!IsPostBack)
       {
           foreach (GridItem item in RadGrid1.MasterTableView.Items)
           {
               if (item is GridEditableItem)
               {
                   GridEditableItem editableItem = item as GridDataItem;
                   editableItem.Edit = true;
               }
           }
           RadGrid1.Rebind();
       }
   }
 
   protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
   {
       if (e.CommandName == "UpdateAll")
       {
           foreach (GridEditableItem editedItem in RadGrid1.EditItems)
           {
               Hashtable newnewValues = new Hashtable();           
               e.Item.OwnerTableView.ExtractValuesFromItem(newnewValues, editedItem);        
               editedItem.Edit = false;
           }
       }
       RadGrid1.Rebind();
   }

Thanks,
Princy
0
ian lipsky
Top achievements
Rank 1
answered on 09 Aug 2013, 06:37 PM
Thanks - i'll try to test it out a little later. I had to remove the grid and go with a standard .net grid just to get things working. I'll try to get back to this later today and test your code and see if there's any difference.

thanks
Tags
Grid
Asked by
ian lipsky
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
ian lipsky
Top achievements
Rank 1
Share this question
or