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

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

5 Answers 794 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ida
Top achievements
Rank 1
Ida asked on 15 Jan 2015, 03:19 PM
Hello,

Could you please advise? I get this error when trying to run my webpage.

The RadGrid code is:

<telerik:RadGrid ID="gridSurveys" runat="server" AutoGenerateDeleteColumn="True" AllowAutomaticInserts="True" AutoGenerateEditColumn="True" OnNeedDataSource="gridSurveys_NeedDataSource" OnItemDataBound="gridSurveys_ItemDataBound" AutoGenerateColumns="False" CellSpacing="-1" GridLines="Both">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="VesselDescription" HeaderText="Vessel" UniqueName="VesselDescription" ReadOnly ="true"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="VesselID" DataType="System.Int32" FilterControlAltText="Filter VesselID column" HeaderText="VesselID" SortExpression="VesselID" UniqueName="VesselID" Visible="false" ReadOnly="True"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="CruiseID" DataType="System.Int32" FilterControlAltText="Filter CruiseID column" HeaderText="Cruise" SortExpression="CruiseID" UniqueName="CruiseID" ReadOnly="True"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Year" DataType="System.Int32" FilterControlAltText="Filter Year column" HeaderText="Year" ReadOnly="True" SortExpression="Year" UniqueName="Year"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Amalgamation" HeaderText="Amalgamation" ReadOnly="True" UniqueName="Amalgamation" Visible ="false"></telerik:GridBoundColumn>
            <telerik:GridDropDownColumn DataField="TargetSurveyDescription" HeaderText="Target Survey" ListTextField="TargetSurveyDescription" ListValueField="TargetSurveyID" UniqueName="TargetSurveyDescription" ColumnEditorID="TargetSurveyDescription" DropDownControlType="DropDownList" Visible="false"></telerik:GridDropDownColumn>
            <telerik:GridBoundColumn DataField="TargetSurveyID" DataType="System.Int32" HeaderText="TargetSurveyID" UniqueName="TargetSurveyID" ReadOnly="true" Visible ="false">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
    </telerik:RadGrid>

And my VB.NET code for binding the dropdownlist is:

Protected Sub gridSurveys_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles gridSurveys.ItemDataBound
       ' Build up the Target Survey dropdown
       Dim editItemTargetSurvey As GridEditableItem = DirectCast(e.Item, GridEditableItem)
       Dim editManagerTargetSurvey As GridEditManager = editItemTargetSurvey.EditManager
       Dim editorTargetSurvey As GridDropDownListColumnEditor = DirectCast(editManagerTargetSurvey.GetColumnEditor("TargetSurveyDescription"), GridDropDownListColumnEditor)
       Dim dsTargetSurvey As New DSSurveyTableAdapters.spGetTargetSurveyTableAdapter
       Dim selectedTargetSurvey As Integer = DataBinder.Eval(editItemTargetSurvey.DataItem, "TargetSurveyID")
       Dim rComboBoxTargetSurvey As DropDownList = editorTargetSurvey.DropDownListControl
       ' Set the width of the dropdown
       rComboBoxTargetSurvey.Width = Unit.Pixel(165)
       ' Populate the dropdown
       rComboBoxTargetSurvey.DataTextField = "TargetSurveyDescription"
       rComboBoxTargetSurvey.DataValueField = "TargetSurveyID"
       rComboBoxTargetSurvey.DataBind()
       rComboBoxTargetSurvey.SelectedValue = selectedTargetSurvey
   End Sub

5 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 20 Jan 2015, 07:52 AM
Hello Ida,

You will need to wrap your logic within an IF condition which checks whether the item is in edit mode:
( Section Accessing controls in edit/insert mode )
http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html


Hope this helps. Please add the missing IF statement and let me know about the result.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Ida
Top achievements
Rank 1
answered on 20 Jan 2015, 09:53 AM
Thank you for your help Eyup.

I have changed my VB.NET code to:

Protected Sub gridSurveys_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles gridSurveys.ItemDataBound
 
    If TypeOf e.Item Is GridDataItem Then
        Dim dataItemTargetSurvey As GridDataItem = CType(e.Item, GridDataItem)
    ElseIf TypeOf e.Item Is GridEditFormItem Then
        Dim editItemTargetSurvey As GridEditFormItem = CType(e.Item, GridEditFormItem)
        Dim editManagerTargetSurvey As GridEditManager = editItemTargetSurvey.EditManager
        Dim editorTargetSurvey As GridDropDownListColumnEditor = DirectCast(editManagerTargetSurvey.GetColumnEditor("TargetSurveyDescription"), GridDropDownListColumnEditor)
        Dim dsTargetSurvey As New DSSurveyTableAdapters.spGetTargetSurveyTableAdapter
        Dim selectedTargetSurvey As Integer = DataBinder.Eval(editItemTargetSurvey.DataItem, "TargetSurveyID")
        ' Build up the Target Survey dropdown
        Dim rComboBoxTargetSurvey As DropDownList = editorTargetSurvey.DropDownListControl
        ' Set the width of the dropdown
        rComboBoxTargetSurvey.Width = Unit.Pixel(165)
        ' Populate the dropdown
        rComboBoxTargetSurvey.DataTextField = "TargetSurveyDescription"
        rComboBoxTargetSurvey.DataValueField = "TargetSurveyID"
        rComboBoxTargetSurvey.DataBind()
        rComboBoxTargetSurvey.SelectedValue = selectedTargetSurvey
    End If
 
End Sub


The original error has gone and I get the following error message when I load the page:

Editor cannot be initialized for column: TargetSurveyDescription
0
Eyup
Telerik team
answered on 23 Jan 2015, 07:43 AM
Hello Ida,

Please try to access the rComboBoxTargetSurvey dropdownlist using the Controls(0) approach as demonstrated in the mentioned section in my previous reply instead of using the editor logic.

That should resolve the issue. Looking forward to your reply.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Ida
Top achievements
Rank 1
answered on 02 Feb 2015, 12:06 PM
Hello Eyup,

I am sorry to ask you about clarifying the answer - I am unsure about how to use the Controls(0) approach instead of the editor logic, from looking at the information at the link you posted up previously.

I have changed my VB.NET code to:

Protected Sub gridSurveys_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles gridSurveys.ItemDataBound
    If (TypeOf e.Item Is GridEditableItem AndAlso CType(e.Item, GridEditableItem).IsInEditMode) Then
 
        Dim editedItemTargetSurvey As GridEditableItem = CType(e.Item, GridEditableItem)
        Dim editManagerTargetSurvey As GridEditManager = editedItemTargetSurvey.EditManager
        Dim editorTargetSurvey As GridDropDownListColumnEditor = CType(editManagerTargetSurvey.GetColumnEditor("TargetSurveyDescription"), GridDropDownListColumnEditor)
 
        Dim dsTargetSurvey As New DSSurveyTableAdapters.spGetTargetSurveyTableAdapter
        Dim selectedTargetSurvey As Integer = DataBinder.Eval(editedItemTargetSurvey.DataItem, "TargetSurveyID")
        Dim ddTargetSurvey As DropDownList = editorTargetSurvey.DropDownListControl
        editorTargetSurvey.DataSource = dsTargetSurvey
 
        ' Populate the dropdown
        ddTargetSurvey.DataTextField = "TargetSurveyDescription"
        ddTargetSurvey.DataValueField = "TargetSurveyID"
        ddTargetSurvey.DataBind()
        ddTargetSurvey.SelectedValue = selectedTargetSurvey
    End If
End Sub


I get the following error message when I click the Edit link on the RadGrid:

An exception of type 'System.ArgumentOutOfRangeException' occurred in System.Web.dll but was not handled in user code

Additional information: 'DDL_TargetSurveyDescription' has a SelectedValue which is invalid because it does not exist in the list of items.


Could you please advise?

Many thanks, Ida
0
Eyup
Telerik team
answered on 05 Feb 2015, 11:24 AM
Hello Ida,

You can use the following approach:
Dim textBox As TextBox = CType(editableItem("ColumnUniqueName").Controls(0), TextBox)

In the mentioned article:
( Section Accessing controls in edit/insert mode )
http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html


If you still have difficulties accessing the generated DropDownList, I can send you a sample web site demonstrating the suggested approach.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Ida
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Ida
Top achievements
Rank 1
Share this question
or