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

Bind Radcombo in multirow edit mode in template column from code-behind with selected value

2 Answers 114 Views
Grid
This is a migrated thread and some comments may be shown as answers.
KorsG
Top achievements
Rank 1
KorsG asked on 12 Feb 2010, 02:32 PM
Hi,

I would like to bind Radcombo boxes in the grid from code-behind, because of the flexibility. 
I want the combo to be related to another combo in the grid, but first i have problems with setting the selected value of the combo. 

Here's a snippet of the grid columns:
                              <telerik:GridTemplateColumn UniqueName="CustomersTemplate" HeaderText="Kunde"
                                    <ItemTemplate> 
                                       <%#DataBinder.Eval(Container.DataItem, "CustID")%> 
                                    </ItemTemplate> 
                                    <EditItemTemplate> 
                                        <telerik:RadComboBox AutoPostBack="true" Width="200px" ID="CustomersDropDown" runat="server" 
                                                DataTextField="Company" DataValueField="CustID"  
                                                AppendDataBoundItems="True" Skin="GiveNH3_default" 
                                                ChangeTextOnKeyBoardNavigation="true" EnableLoadOnDemand="true" MarkFirstMatch="true" EnableEmbeddedSkins="false" 
                                                > 
                                                <Items> 
                                                    <telerik:RadComboBoxItem runat="server" Text=""  
                                                        Value="0" /> 
                                                </Items> 
                                        </telerik:RadComboBox> 
                                    </EditItemTemplate> 
                                </telerik:GridTemplateColumn> 

And of the code-behind:
Protected Sub Grid_ItemDataBound(ByVal sender As ObjectByVal e As GridItemEventArgs) Handles Grid.ItemDataBound 
  If (TypeOf e.Item Is GridEditableItem AndAlso CType(e.Item, GridEditableItem).IsInEditMode) Then 
Using C As RadComboBox = CType(item.FindControl("CustomersDropDown"), RadComboBox) 
                Dim dt As New DataTable 
        Dim _CustTypeID = 1 'This could be different for each combo, and would be set from another combo in the grid 
                Using cmd As New SqlCommand("SELECT * FROM Customers WHERE CustTypeID = " & _CustTypeID  & "", GetSQLConnection) 
                    Using dr = cmd.ExecuteReader 
                        dt.Load(dr) 
                    End Using 
                End Using 
                C.DataSource = dt 
                C.DataBind() 
                Dim editItem = CType(e.Item, GridEditableItem) 
 
                'This doesn't return the corret value from saved value in the database - instead it returns the first value of the datasource 
                Dim s = DataBinder.Eval(editItem.DataItem, "CustID").ToString 
                C.SelectedValue = s 
 
            End Using 
End if 
End sub 


Can you point me in the right direction? 

Any help appreciated!


( I use v. 2009.3.1314.35 of the web.ui)

2 Answers, 1 is accepted

Sort by
0
Accepted
Veli
Telerik team
answered on 17 Feb 2010, 01:18 PM
Hello KorsG,

You have taken the right approach and your code seems fine. Do you get any exceptions? Note that you need to have a combo item whose value is equal to the selected value you are trying to set.

Kind regards,
Veli
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
KorsG
Top achievements
Rank 1
answered on 25 Feb 2010, 07:04 PM
Hi Veli,

Thanks for your feedback. 
In the meantime i somehow got it to work, so you were right - the code was fine :-)


Tags
Grid
Asked by
KorsG
Top achievements
Rank 1
Answers by
Veli
Telerik team
KorsG
Top achievements
Rank 1
Share this question
or