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

Casting issue when edit

2 Answers 126 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Purushottam Somani
Top achievements
Rank 2
Purushottam Somani asked on 13 Sep 2010, 12:47 PM
I am running this example

http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/xmldatasource/defaultcs.aspx

but getting this error "Unable to cast object of type 'Telerik.Web.UI.GridDataItem' to type 'Telerik.Web.UI.GridEditFormItem'
" on bold line

protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
    {
        GridEditFormItem gridEditFormItem = (GridEditFormItem)e.Item;
         Hashtable ht = new Hashtable();
        gridEditFormItem.ExtractValues(ht);



aspx code is like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EditAll.aspx.cs" Inherits="EditAll" %>

<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Test Page</title>
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">

        <script type="text/javascript">
        <!--
         function RowDblClick(sender, eventArgs)
         {
             sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
         }
      
     -->    
        </script>

    </telerik:RadCodeBlock>
</head>
<body>
    <form runat="server" id="mainForm" method="post">
    <div id="divMsg" runat="server" style="float: left;">
        <asp:Label ID="lblMsg" runat="server" ForeColor="Red"></asp:Label>
    </div>
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <!-- content start -->
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                     <telerik:AjaxUpdatedControl ControlID="divMsg" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadGrid ID="RadGrid1" Width="97%" DataSourceID="XmlDataSource1" EnableAjaxSkinRendering="true"
        ShowStatusBar="True" AllowSorting="True" PageSize="7" GridLines="None" AllowPaging="True"
        runat="server" OnUpdateCommand="RadGrid1_UpdateCommand">
        <MasterTableView AutoGenerateColumns="false" DataKeyNames="id" EditMode="InPlace" Width="100%">
            <Columns>
                <telerik:GridBoundColumn UniqueName="id" DataField="id" HeaderText="ID" ReadOnly="True"
                    HeaderStyle-Width="10%" />
                <telerik:GridBoundColumn UniqueName="Name" DataField="Name" HeaderText="Name" HeaderStyle-Width="25%"
                    ColumnEditorID="GridTextBoxColumnEditor1" />
                <telerik:GridDropDownColumn UniqueName="Class" HeaderText="Class" ColumnEditorID="GridDropDownListColumnEditor1"
                    ListTextField="Class" ListValueField="Class" DataSourceID="XmlDataSource1" DataField="Class"
                    HeaderStyle-Width="10%" />
                <telerik:GridNumericColumn UniqueName="Salary" DataField="Salary" HeaderText="Salary"
                    DataFormatString="{0:C}" HeaderStyle-Width="10%" ColumnEditorID="GridNumericColumnEditor1" />
                <telerik:GridEditCommandColumn UniqueName="Edit" HeaderText="Edit All">
                </telerik:GridEditCommandColumn>
            </Columns>
        </MasterTableView>
        <ClientSettings>
            <ClientEvents OnRowDblClick="RowDblClick" />
        </ClientSettings>
    </telerik:RadGrid>
    <telerik:GridTextBoxColumnEditor ID="GridTextBoxColumnEditor1" runat="server" TextBoxStyle-Width="180px" />
    <telerik:GridNumericColumnEditor ID="GridNumericColumnEditor1" runat="server" NumericTextBox-Width="50px" />
    <telerik:GridDropDownListColumnEditor ID="GridDropDownListColumnEditor1" runat="server"
        DropDownStyle-Width="70px" />
    <br />
    <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/XML/Customer.xml">
    </asp:XmlDataSource>
    <!-- content end -->
    </form>
</body>
</html>

Is there way to cast directly, to resolve this error?

Thanks

.

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 13 Sep 2010, 01:11 PM
Hello Purushottam,

Since you are using 'Inplace' edit mode, the grid row is of type GridEditableItem in edit mode. Hence you can cast the item in the UpdateCommand event to those types according to its current mode:

C#:
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
  {
    GridEditableItem editItem = (GridEditableItem)e.Item;
  }

Thanks,
Princy.
0
Purushottam Somani
Top achievements
Rank 2
answered on 14 Sep 2010, 08:22 AM
Thanks, it works.......
Tags
Grid
Asked by
Purushottam Somani
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Purushottam Somani
Top achievements
Rank 2
Share this question
or