Select multiple items in RadComboBox programatically

2 posts, 0 answers
  1. Richard
    Richard avatar
    6 posts
    Member since:
    Jan 2017

    Posted 23 May Link to this post

    I have a RadComboBox in WPF that is set to allow for multiple selection.  I can manually select multiple items and I can get the set of selected items but I can't figure out how to programmatically select multiple items.   The value in the RadComboBox is bound to a DataTable with the value and text both being linked to the same column.  Sample values for the data are (00, 01, 02, 03, 04, 05, 06, 07).  How can I take a comma delimited string (example: "01, 03, 06") and select the those items from that string in the RadComboBox?

    <DataTemplate x:Key="MultipleSelectionBoxTemplateDept">
    <TextBlock Text="{Binding ElementName=cmbDept, Path=SelectedItems.Count, StringFormat='Selected Items Count: {0}'}" Foreground="Red" FontWeight="Bold" />
    </DataTemplate>

    <telerik:RadComboBox Name="cmbDept" AllowMultipleSelection="True" IsEditable="False" Width="250" Height="25" Margin="0,0,0,0" VerticalAlignment="Top" ItemsSource="{Binding}" MultipleSelectionBoxTemplate="{StaticResource MultipleSelectionBoxTemplateDept}" OpenDropDownOnFocus="True"></telerik:RadComboBox>

     

    C# code:

    cmbDept.DataContext = dtDeptList.DefaultView;
    cmbDept.DisplayMemberPath = dtDeptList.Columns[0].ToString();
    cmbDept.SelectedValuePath = dtLeadList.Columns[0].ToString();

  2. Richard
    Richard avatar
    6 posts
    Member since:
    Jan 2017

    Posted 24 May in reply to Richard Link to this post

    I got it to work.  I had not even thought of looking for the value in the underlying datatable and getting the index that way.  My working code is below.

    foreach (string s in myDepts)
    {
       for (int x = 0; x < dtDeptList.Rows.Count - 1; x++)
       {
          if (dtDeptList.Rows[x][0].ToString() == s)
          {
            cmbDept.SelectedItems.Add(cmbDept.Items[x]);
          }
       }                           
    }

Back to Top