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

Binding drop down list in form template in code behind

5 Answers 319 Views
Grid
This is a migrated thread and some comments may be shown as answers.
andieje
Top achievements
Rank 1
andieje asked on 28 Jul 2009, 02:38 PM
Hi

Please can you tell me how i would bind a drop down list within the form template of an edit form settings in the code behind. I have seen how to do it in the aspx like so


                                         <td> 
         <asp:DropDownList ID="ddlTOC" runat="server" SelectedValue='<%# Bind("TitleOfCourtesy") %>' 
                 DataSource='<%# (new string[] { "Dr.", "Mr.", "Mrs.", "Ms." }) %>' TabIndex="7" 
     AppendDataBoundItems="True">  
        <asp:ListItem Selected="True" Text="Select" Value=""></asp:ListItem> 
  </asp:DropDownList></td

but not in the code behind

many thanks

5 Answers, 1 is accepted

Sort by
0
andieje
Top achievements
Rank 1
answered on 28 Jul 2009, 10:13 PM
HI

I found this in the help topics. Thanks
0
andieje
Top achievements
Rank 1
answered on 29 Jul 2009, 12:49 PM
Hi

I thought i had got this working but apparently not.

I have a drop down list inside an edit form template. I believe the radgrid automatically uses the same edit form template to insert and update a record. I am populating a drop down list and setting its selected value according to the value of the current row like so

Protected Sub gridCustomers_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles gridCustomers.ItemDataBound  
        If TypeOf e.Item Is GridDataItem Then  
                        
        ElseIf (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) Then  
 
 
            Dim editFormItem As GridEditFormItem = CType(e.Item, GridEditFormItem)  
            Dim c As Customers = CType(e.Item.DataItem, Customers)  
            Dim list As DropDownList = CType(editFormItem.FindControl("cboTitle"), DropDownList)  
            Dim titles As New TitlesCollection  
            titles.LoadAll()  
            list.DataSource = titles 
            list.DataBind()  
            list.SelectedValue = c.ContactTitleID  
 
        End If 

This works fine when the user is updating a record.However when the user is inserting a new record i get this error

Unable to cast object of type 'Telerik.Web.UI.GridInsertionObject' to type 'SureTrack2.Data.BusinessObjects.Customers'.  

How do i distinguish between when the user is updating a record and inserting a new one?

thanks
0
Yavor
Telerik team
answered on 03 Aug 2009, 07:38 AM
Hi andieje,

The reason for the exception in question is that when you are inserting records in the grid control, the
"edit form" is of type GridEditFormInsertItem, rather than GridEditFormItem. You can check whether the grid is in insert mode, by checking the RadGrid1.MasterTableView.IsItemInserted property.
I hope this helps.

Greetings,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
andieje
Top achievements
Rank 1
answered on 07 Aug 2009, 11:15 AM
Hi

I'm sorry but i don't understand that answer. Code that is inside this IF statement
If (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) Then
is executed when the user performs an edit or an insert so if the insert form is of type GridEditFormInsertItem, how can the above if statement return true?
thanks
0
Yavor
Telerik team
answered on 07 Aug 2009, 12:50 PM
Hi andieje,

Indeed, based on the code, the issue should not be present. However, I based my suggestion on the type of exception. If the issue still persists, you can open a formal support ticket, and send us the application for additional review and testing.

Sincerely yours,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
andieje
Top achievements
Rank 1
Answers by
andieje
Top achievements
Rank 1
Yavor
Telerik team
Share this question
or