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 Object, ByVal 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)