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

Select multiple items in RadComboBox programatically

1 Answer 925 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 23 May 2017, 04:11 PM

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();

1 Answer, 1 is accepted

Sort by
0
Richard
Top achievements
Rank 1
answered on 24 May 2017, 12:11 PM

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]);
      }
   }                           
}

Tags
ComboBox
Asked by
Richard
Top achievements
Rank 1
Answers by
Richard
Top achievements
Rank 1
Share this question
or