Unable to cast object of type "Telerik.Web.UI.GridDataItem" to type "Telerik.Web.UI.GridEditFormItem"

3 posts, 1 answers
  1. io
    io avatar
    6 posts
    Member since:
    Jan 2020

    Posted 13 Feb Link to this post

    Hello

     

    I have a radgrid which allows the user to select a row with 2 methods: by clicking on the row and by clicking a button in the row, so the user can edit or delete the information in those rows and to display information in another radgrid related to the row selected (Radgrid1 = Requests, Radgrid2 = Details, so if Request #2 is selected, the details of that requests are displayed in the other grid)

     

    I have no issues if the user uses the "click on the row method", but if the user tries using the button it shows me the error:

         "Unable to cast object of type 'Telerik.Web.UI.GridDataItem' to type 'Telerik.Web.UI.GridEditFormItem'"

     

    The button method by itself works, it still updates, deletes and populates the second grid in a correct way, but if the user edits, it doesnt show the results until the radgrid is refreshed or until the row is selected again. I need help to fix that, even if it still works in some way.

     

    RadGrid1

    <asp:UpdatePanel ID="upPeticiones" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
                        <ContentTemplate>
                            <telerik:RadGrid ID="rgPeticiones" runat="server" GroupingSettings-CaseSensitive="false"  AllowPaging="True" RenderMode="Lightweight"
                                AllowSorting="true" AllowFilteringByColumn="True"  PageSize="5 " OnNeedDataSource="rgPeticiones_NeedDataSource" OnItemCommand="ItemCommand" OnItemDataBound="rgPeticiones_ItemDataBound" OnSelectedIndexChanged="rgPeticiones_SelectedIndexChanged"
                                    CssClass="RadGrid_Silk" EnableHeaderContextMenu="true" GridLines="Horizontal" Width="100%" AllowAutomaticDeletes="True" AllowAutomaticInserts="False" AllowAutomaticUpdates="False" AllowMultiRowSelection="false" MasterTableView-AllowSorting="false">
                                <ClientSettings Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="true">
                                </ClientSettings>
                                <HeaderContextMenu Enabled="false"></HeaderContextMenu>
                                <MasterTableView AutoGenerateColumns="false" DataKeyNames="Folio" ItemStyle-HorizontalAlign="Center" AlternatingItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" NoMasterRecordsText="<%$ Resources:Language, lbl_tablaVacia %>" CommandItemDisplay="Top"  EditMode="PopUp" EditFormSettings-CaptionFormatString="<%$ Resources:Language, lbl_rg_termino %>">
                                    <CommandItemSettings ShowAddNewRecordButton="false" AddNewRecordText="" RefreshText="<%$ Resources:Language, lbl_actualizar %>"/>
                                    <Columns>
                                        <telerik:GridBoundColumn DataField="Folio" HeaderText="<%$ Resources:Language, lbl_rg_folio %>" Display="true" ReadOnly="true" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="false" FilterControlWidth="100%" HeaderStyle-HorizontalAlign="Center" DataType="System.String" AllowSorting="false"></telerik:GridBoundColumn>

                                        .....

     

                                        (Button for selection)
                                        <telerik:GridTemplateColumn DataField="" HeaderText=""  HeaderStyle-Width="3%"  AllowFiltering="false" UniqueName="Detalles">
                                            <ItemTemplate>
                                                <asp:ImageButton  ID="imgDetalle" runat="server" CausesValidation="False" CommandArgument='<%# Eval("Folio") %>' CommandName="Detalle" ImageUrl="~/images/detail.png" ItemStyle-Wrap="True"  ToolTip="<%$ Resources:Language, lbl_rg_detalle %>" OnClick="imgDetalle_Click" OnClientClick="deselectrows();"/>
                                            </ItemTemplate>
                                            <ItemStyle HorizontalAlign="Center" Width="3%" />
                                        </telerik:GridTemplateColumn>

                                        
                                    </Columns>
                                </MasterTableView>
                            </telerik:RadGrid>
                        </ContentTemplate>
                    </asp:UpdatePanel>

     

    RadGrid2

     <asp:UpdatePanel ID="upNota" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
                        <ContentTemplate>
                            <telerik:RadGrid ID="rgNota" runat="server" GroupingSettings-CaseSensitive="false"  AllowPaging="True" RenderMode="Lightweight"
                                AllowSorting="true" AllowFilteringByColumn="True"  PageSize="10" OnNeedDataSource="rgNota_NeedDataSource"  OnItemDataBound="rgNota_ItemDataBound"
                                    CssClass="RadGrid_Silk" EnableHeaderContextMenu="false" GridLines="Horizontal" Width="100%"  MasterTableView-EditFormSettings-FormTableButtonRowStyle-HorizontalAlign="Right">
                                <HeaderContextMenu Enabled="false"></HeaderContextMenu>
                                <MasterTableView AutoGenerateColumns="false" DataKeyNames="Folio" ItemStyle-HorizontalAlign="Center" AlternatingItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" NoMasterRecordsText="<%$ Resources:Language, lbl_tablaVacia %>" CommandItemDisplay="Top">
                                    <CommandItemSettings ShowAddNewRecordButton="false" ShowRefreshButton="false"/>
                                    <EditFormSettings  FormTableStyle-HorizontalAlign="Right" FormTableButtonRowStyle-HorizontalAlign="Right" >
                                    </EditFormSettings>
                                    <Columns>
                                        
                                        <telerik:GridBoundColumn DataField="Folio" HeaderText="<%$ Resources:Language, lbl_rg_folio %>" Display="false" ReadOnly="true" AutoPostBackOnFilter="true" AllowFiltering="false" CurrentFilterFunction="EqualTo" ShowFilterIcon="false" FilterControlWidth="100%" HeaderStyle-HorizontalAlign="Center" DataType="System.String">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn DataField="Nota" HeaderText="<%$ Resources:Language, lbl_rg_nota %>" Display="true" AllowFiltering="false" ReadOnly="true" AutoPostBackOnFilter="true" CurrentFilterFunction="contains" ShowFilterIcon="false" FilterControlWidth="100%" HeaderStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
                                        
                                    </Columns>
                                </MasterTableView>
                            </telerik:RadGrid>
                        </ContentTemplate>
                    </asp:UpdatePanel>

     

    protected void ItemCommand(object source, GridCommandEventArgs e)
            {
                try
                {
                    int Folio;
                    int.TryParse(e.CommandArgument.ToString(), out Folio);

                    if (e.CommandName.Equals("Delete")) this.eliminaPeticion(Folio);
                    else if (e.CommandName.Equals("Update"))
                    {
                        int.TryParse(((GridEditFormItem)e.Item).GetDataKeyValue("Folio").ToString(), out Folio);
                        this.rgPeticiones_ItemUpdated(Folio, (GridEditFormItem)e.Item);
                    }
                    else if (e.CommandName.Equals("Detalle"))
                    {
                        int.TryParse(((GridEditFormItem)e.Item).GetDataKeyValue("Folio").ToString(), out Folio); *Here is where I get the Error Message
                        this.rgPeticiones_ItemUpdated(Folio, (GridEditFormItem)e.Item);
                    }
                }
                catch (Exception ex) { this.ShowMessage("Err. grid rgPeticiones." + ex.Message, "danger"); }
            }

     

    I have another error showing, this time in the NeedDataSource when the user tries to update the second RadGrid with the button method

    "Index was out of range. Must be non-negative and less than the size of the collection parameter name:index"

     

    protected void rgNota_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
            {
                try
                {
                    Telerik.Web.UI.GridDataItem item = (Telerik.Web.UI.GridDataItem)rgPeticiones.SelectedItems[0]; *Here is where I get the second error
                    foreach (Telerik.Web.UI.GridDataItem items in rgPeticiones.SelectedItems)
                    {
                        var Folio = items["Folio"].Text;
                        int FolioInt = int.Parse(Folio.ToString());
                        try
                        {
                            rgDetalles.DataSource = CDatos.SP_PeticionesAlmacen.SP_ObtenerDetalle(FolioInt);
                            upDetalles.Update();
                        }
                        catch (Exception ex) { this.ShowMessage("Err. load grid rgDetalles." + ex.Message, "danger"); }
                    }
                }
                catch (Exception ex) { this.ShowMessage("Err. load grid rgDetalles." + ex.Message, "danger"); }
            }

     

    The method works perfectly if the "Click on row" method is used

     

    Would be possible to have a "false button"? An image in the radgrid for the user just to click, selecting the row? I have tried with a "telerik:GridImageColumn" in my radgrid, it display the image but doesnt select the row if its clicked

  2. io
    io avatar
    6 posts
    Member since:
    Jan 2020

    Posted 13 Feb in reply to io Link to this post

    I made a mistake while copying the NeedDataSource method, it should be like this:

     

    protected void rgNota_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
            {
                try
                {
                    Telerik.Web.UI.GridDataItem item = (Telerik.Web.UI.GridDataItem)rgPeticiones.SelectedItems[0]; *Here is where I get the second error
                    foreach (Telerik.Web.UI.GridDataItem items in rgPeticiones.SelectedItems)
                    {
                        var Folio = items["Folio"].Text;
                        int FolioInt = int.Parse(Folio.ToString());
                        try
                        {
                            rgNota.DataSource = CDatos.SP_PeticionesAlmacen.SP_ObtenerNota(FolioInt);
                            upNota.Update();
                        }
                        catch (Exception ex) { this.ShowMessage("Err. load grid rgNota." + ex.Message, "danger"); }
                    }
                }
                catch (Exception ex) { this.ShowMessage("Err. load grid rgNota." + ex.Message, "danger"); }
            }

  3. Answer
    Attila Antal
    Admin
    Attila Antal avatar
    571 posts

    Posted 18 Feb Link to this post

    Hi,

    The reason for the error message is that the code tries to cast a GridDataItem to a GridEditFormItem, while the two items are not are of the same type.

    Having in mind that the Button is in a GridDataItem at the moment you click on it, in the ItemCommand the "e.Item" can only be converted to "GridDataItem" or "GridItem".

    Check out the RadGrid Structure article to get a better understanding of the different items in the Grid. Also, check out the ItemCommand Event to find out more about the command event.

    For examples about accessing Items, controls, cells, and values in RadGrid, visit the Accessing Values and Controls article.

    The articles above will help you learn more about the item types and how to access them, or how to access controls in them. That will help you implement the required behavior.

    Kind regards,
    Attila Antal
    Progress Telerik

    Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
Back to Top