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

[Solved] RadGrid Not Obtaining/Remembering The Selected Value of a Templated Field Containing a RadCombobox On Inserts or Edits

5 Answers 415 Views
Grid
This is a migrated thread and some comments may be shown as answers.
jgill
Top achievements
Rank 1
jgill asked on 27 May 2009, 06:39 AM
I have a RadGrid that has a Templated column containing a RadComboBox that is populated with data from a different datasource that the RadGrid's datasource (Example: You have a RadGrid of Products which can have ParentProducts.  When inserting new Products or editing existing Products you want to allow the user to select that the Product has no Parent Product or select a Product from a list of Products as the parent).

I am having an issue with the RadGrid always selecting the ""/Nul/Select an option value even when the user selects a value.  The effect of this is that any new item inserted via the RadGrid always has no Parent and any item with a Parent that is edited ends up having no Parent.

Is there something missing or wrong in the code below?

 <telerik:GridTemplateColumn HeaderText="SectionID" UniqueName="SectionID">  
                        <EditItemTemplate> 
                            <telerik:RadComboBox ID="SectionIDEditor" runat="server" AppendDataBoundItems="true" DataValueField="SectionID" DataTextField="SectionName" DataSourceID="SqlDataSource3" Skin="WebBlue" > 
                            <Items> 
                                <telerik:RadComboBoxItem Value="" Text="Select Parent Section" /> 
                            </Items> 
                            </telerik:RadComboBox> 
                        </EditItemTemplate> 
                        <ItemTemplate> 
                            <asp:Label ID="SectionIDLabel" runat="server" Text='<%# Eval("SectionID") %>'></asp:Label> 
                        </ItemTemplate> 
                    </telerik:GridTemplateColumn> 

    Protected Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound  
        If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then 
 
            'access/modify the edit item template settings here  
            Dim Section As RadComboBox = CType(e.Item.FindControl("SectionIDEditor"), RadComboBox)  
            Section.DataSourceID = "SqlDataSource3" 
            Section.DataBind()  
 
            If Not DataBinder.Eval(e.Item.DataItem, "SectionID"Is Nothing And IsNumeric(DataBinder.Eval(e.Item.DataItem, "SectionID")) Then 
                ''Section.SelectedValue = DirectCast(DataBinder.Eval(e.Item.DataItem, "SectionID"), Integer)  
                ''Section.Items(0).Attributes("style") = "color: red"  
                'Section.SelectedValue = DataBinder.Eval(e.Item.DataItem, "SectionID")  
 
                ''If Section.Items.FindItemByValue("", True).Selected Then  
 
                ''End If  
                ''Dim ComboItem As RadComboBoxItem  
                ''For Each ComboItem In Section.Items  
                ''    If ComboItem.Value.Equals(e.Item.DataItem) Then  
                ''        ComboItem.Selected = True  
                ''    End If  
                ''Next ComboItem  
                If (Not Session("updatedValue"Is NothingThen 
                    Section.SelectedValue = Session("updatedValue")  
                End If 
            Else 
                Section.SelectedValue = "" 
            End If 
 
        End If 
 
        'If (TypeOf e.Item Is GridEditableItem AndAlso CType(e.Item, GridEditableItem).IsInEditMode) Then  
        '    'Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)  
        '    'Dim editMan As GridEditManager = editedItem.EditManager  
        '    'Dim editor As GridTemplateColumnEditor = CType(editMan.GetColumnEditor("SectionID"), GridTemplateColumnEditor) 'in case you have RadComboBox editor for the GridDropDownColumn (this is the default editor), 'you will need to use ComboBoxControl below instead of DropDownListControl 'and add RadComboBoxItems instead of ListItems to the Items collection of the editor   
        '    'Dim Section As RadComboBox = editor.ComboBoxControl  
 
        '    Dim Section As RadComboBox = CType(e.Item.FindControl("SectionIDEditor"), RadComboBox)  
        '    'Section.Items.Insert(0, New RadComboBoxItem("Select Parent Section", "NotSetItem"))  
        '    If Not DataBinder.Eval(e.Item.DataItem, "SectionID") Is Nothing And IsNumeric(DataBinder.Eval(e.Item.DataItem, "SectionID")) Then  
        '        'Section.SelectedValue = DirectCast(DataBinder.Eval(e.Item.DataItem, "SectionID"), Integer)  
        '        'Section.Items(0).Attributes("style") = "color: red"  
        '        Section.SelectedValue = DataBinder.Eval(e.Item.DataItem, "SectionID")  
 
        '        'If Section.Items.FindItemByValue("", True).Selected Then  
 
        '        'End If  
        '        'Dim ComboItem As RadComboBoxItem  
        '        'For Each ComboItem In Section.Items  
        '        '    If ComboItem.Value.Equals(e.Item.DataItem) Then  
        '        '        ComboItem.Selected = True  
        '        '    End If  
        '        'Next ComboItem  
        '    End If  
 
 
        'End If  
 
    End Sub 

5 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 29 May 2009, 03:13 PM
Hi jgill,

Indeed, with GridTemplateColumn, you need to set the SelectedValue of the RadComboBox manually through code behind, in ItemDataBound event of RadGrid for instance. If you want the grid to handle it automatically I suggest that you use GridDropDownColumn instead.

Please find more information on drop=down controls and RadGrid in the below articles:
http://www.telerik.com/help/aspnet-ajax/grdoperationswithdropdownlistinedititemtemplate.html
http://www.telerik.com/help/aspnet-ajax/grdcustomizeconfiguregriddropdowncolumn.html
http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/columntypes/defaultcs.aspx

Check it out and let me know if this helps.

Regards,
Iana
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
jgill
Top achievements
Rank 1
answered on 29 May 2009, 03:35 PM
Lana,
My code is doing the same thing as http://www.telerik.com/help/aspnet-ajax/grdoperationswithdropdownlistinedititemtemplate.html, including the UpdateCommand code, however I have the problem mentioned in the original post.

1. When I edit a row with a value for the field that is a dropdown (it cannot be a GridDropDownColumn due to the requirements) it successfully selected the value from the combobox on edit.
2. However if for example the selected item in the combobox is left unchanged and I just click save without making any changes the value for the row changes to the Empty/Null selection instead of the item that is selected from the combobox.  This occurs in all cases.
0
jgill
Top achievements
Rank 1
answered on 29 May 2009, 04:06 PM
Lana,
I see the problem. The logic in my code has the selected value always reset to "".

If Not DataBinder.Eval(e.Item.DataItem, "SectionID"Is Nothing And IsNumeric(DataBinder.Eval(e.Item.DataItem, "SectionID")) Then    
            If (Not Session("updatedValue"Is NothingThen    
                    Section.SelectedValue = Session("updatedValue")     
            End If    
Else    
                Section.SelectedValue = ""    
End If   

The code above is the problem.  It should be similar to:

If Not DataBinder.Eval(e.Item.DataItem, "SectionID"Is Nothing And IsNumeric(DataBinder.Eval(e.Item.DataItem, "SectionID")) Then    
            If (Not Session("updatedValue"Is NothingThen    
                    Section.SelectedValue = Session("updatedValue")   
            Else 
                     'Default dropdown/combobox selection  
                     Section.SelectedValue = ""   
            End If    
End If   
0
jgill
Top achievements
Rank 1
answered on 29 May 2009, 06:00 PM
Nevermind...what I posted does not work either.  I am still encountering this problem.
0
Iana Tsolova
Telerik team
answered on 30 May 2009, 11:07 AM
Hi jgill,

Could you please confirm that your grid is bound through the NeedDataSource event as it is in the help article? And tell be how are you updating your database, it is with automatic operations or manually?
I suggest that you paste here the whole UpdateCommand handler for further investigation.

All the best,
Iana
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
jgill
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
jgill
Top achievements
Rank 1
Share this question
or