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

RadComboBox with CheckBoxes inside a DataGrid

1 Answer 333 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Top Coder
Top achievements
Rank 1
Top Coder asked on 18 Jul 2011, 05:41 AM
I'm attempting to have the new RadComboBox with checkboxes="true" bound to a SqlDataSource inside a radGrid. 

It works fine when just 1 item is selected, but when I select multiple Items it doesn't work. I try to convert all the selected countries ids into a comma separated string and then add an updateparameter to update the database.  But it doesn't update it. I'm not sure what I'm doing wrong. 

I'll also like to know the proper way to populate the checkboxes in the radcombobox automatically, when editmode is enabled on the grid.

<telerik:GridTemplateColumn HeaderText="Select Countries" ItemStyle-Width="240px" UniqueName="List_of_Countries" >
            <ItemTemplate>
                <%#  DataBinder.Eval(Container.DataItem, "List_of_Countries")%>
            </ItemTemplate>
            <EditItemTemplate>
                 <telerik:RadComboBox runat="server" ID="rdComboCountries" DataTextField="country_name"
CheckBoxes="true" Width="250px"   DataValueField="country_id" DataSourceID="SqlDataSourceCountries"
SelectedValue='<%#Bind("List_of_Countries") %>'>
                </telerik:RadComboBox>
            </EditItemTemplate>
 </telerik:GridTemplateColumn>


  <asp:SqlDataSource runat="server" ID="SqlDataSourceCountries" ConnectionString="<%$ ConnectionStrings:mydbConnectionString %>"
ProviderName="<%$ ConnectionStrings:mydbConnectionString.ProviderName %>"
    SelectCommand="SELECT country_id, Country_names FROM lu_countries">
    </asp:SqlDataSource>

Private Sub rgCountries_UpdateCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles rgCountries.UpdateCommand
        Dim editItem As GridDataItem = e.Item
 
        'Pass the parameter values to the SqldataSource
         oDSMySqlCountries.UpdateParameters.Add(New Parameter("list_of_countries"))
  
         oDSMySqlCountries.UpdateParameters("list_of_countries").DefaultValue = GetCheckedItems(DirectCast(editItem.FindControl("rdComboCountries"), RadComboBox))
 
        'Update the values
        oDSMySqlCountries.Update()
 
 
        'Bind the RadGrid again
        rgCountries.Rebind()
 End Sub
  
 Private Function GetCheckedItems(ByVal comboBox As RadComboBox) As String
        Dim sb As New StringBuilder()
        Dim collection As IList(Of RadComboBoxItem) = comboBox.CheckedItems
  
        For Each item As RadComboBoxItem In collection
            sb.Append(item.Value + ", ")
        Next
 
         Return sb.ToString.TrimEnd(" ").TrimEnd(",")
     End Function

1 Answer, 1 is accepted

Sort by
0
Kalina
Telerik team
answered on 21 Jul 2011, 04:23 PM
Hi Top Coder,

Please pass the RadComboBox checked items texts as a SqlDataSource.UpdateParameter in this way:
Protected Sub RadGrid1_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs)
 
    Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
    Dim comboBox As RadComboBox = DirectCast(editedItem.FindControl("RadComboBox1"), RadComboBox)
    SqlDataSource1.UpdateParameters.Add(New Parameter("SelectedCountries", DbType.[String], comboBox.Text))
    SqlDataSource1.UpdateParameters.Add(New Parameter("Name", DbType.[String], TryCast(e.Item.FindControl("NameBox"), TextBox).Text))
    SqlDataSource1.UpdateParameters.Add(New Parameter("ID", DbType.Int32, TryCast(e.Item.FindControl("IDBox"), TextBox).Text))
    SqlDataSource1.Update()
 
End Sub

In order to set the text of the checked items in RadComboBox input when the RadGrid is in "Edit mode" - please handle the RadGrid.OnItemDataBoundHandler event:
Protected Sub OnItemDataBoundHandler(ByVal sender As Object, ByVal e As GridItemEventArgs)
       If e.Item.IsInEditMode Then
           Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
           If Not (TypeOf e.Item Is IGridInsertItem) Then
               Dim combo As RadComboBox = DirectCast(item.FindControl("RadComboBox1"), RadComboBox)
 
               Dim dr As DataRowView = DirectCast(e.Item.DataItem, DataRowView)
               Dim countries As String = dr("SelectedCountries").ToString()
 
               If countries.Length > 0 Then
                   Dim ids As String() = countries.Split(",")
                   For i As Integer = 0 To ids.Length - 1
                combo.Items.FindItemByText(ids(i).Trim()).Checked = True
 
                   Next
 
 
               End If
           End If
       End If
   End Sub

I prepared an example for you – please find it attached.


Kind regards,
Kalina
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
Grid
Asked by
Top Coder
Top achievements
Rank 1
Answers by
Kalina
Telerik team
Share this question
or