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

set selected value of combobox from ItemSource bound collection property arriving from database.

2 Answers 113 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Hazzard
Top achievements
Rank 1
Hazzard asked on 02 Nov 2011, 12:40 AM

Given the xaml below, I'm populating a combobox within the gridview with a Collection of data that represents the two possible 'valid' values from the database.  Two questions.

  1. How do I set the value of combobox to the same value as it arrives from the database query and is bound to the gridview that contains the combobox and
  2. If the value from the database for the column/field/property 'Class' is 'NA' or null, I don't want a combobox to be created, or at least have it appear to the user that it is an empty cell.  (There will be nothing to select.)

 

<l:Collection x:Key="EMC_Classes">
    <!-- Collection<object> -->
    <l:EMC_Class ID="1" Class="A or B" />
    <l:EMC_Class ID="2" Class="A,B,C,D" />
</l:Collection>
  
<l:ValueToItemConverter x:Key="EMCID2Class"
                    ItemsSource="{StaticResource EMC_Classes}"
                    ValuePath="ID"
                    DisplayMemberPath="Class" />
  
  
<telerik:RadGridView Name="rgvEMCTestPlans"
  
<telerik:GridViewColumn Header="Class" Width="100" IsReadOnly="False" x:Name="ComboClass">
    <telerik:GridViewColumn.CellTemplate>
        <DataTemplate >
            <ComboBox FontSize="10"   Width="90" HorizontalAlignment="Center" 
                    ItemsSource="{StaticResource EMC_Classes}"
                    DisplayMemberPath="Class"
                    SelectedValuePath="ID"
                    SelectedValue="1"
                    SelectedIndex="1"
                    SelectedItem="1"
                    SelectionChanged="ComboBox_SelectionChanged"
                />
        </DataTemplate>
    </telerik:GridViewColumn.CellTemplate>
</telerik:GridViewColumn>
public void GridLoaded(object sender, TypeLoadedEventArgs e)
{
   MyAppliedStandardsModels = new ObservableCollection<AppliedStandardsModel>();
   var emcClasses = new List<EMC_Class>();
   foreach (AppliedStandardsModel item in e.SType)
   {
      MyAppliedStandardsModels.Add(item);
   }
      rgvEMCTestPlans.ItemsSource = MyAppliedStandardsModels;
   }
  
  
 public class EMC_Class
 {
    public string Class { get; set; }
    public int ID { get; set; }
 }
  
private ObservableCollection<AppliedStandardsModel> _myAppliedStandardsModel;
  
public ObservableCollection<AppliedStandardsModel> MyAppliedStandardsModels
{
    get { return _myAppliedStandardsModel; }
    set
    {
        _myAppliedStandardsModel = value;
        FirePropertyChanged("MyAppliedStandardsModel");
    }
}

 
Class values flow

2 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 02 Nov 2011, 07:27 AM
Hi Greg,

Generally, my commendation would be to work directly with GridViewComboBoxColumn. You can take a look at our online documentation and demos for a reference. You need to define the column's DataMemberBinding as otherwise the grid will not be able sort, group, filter or find the appropriate property from the corresponding business object. 
Considering your second requirement, you can create a style targeting RadComboBox and set its EmptyText property just as illustrated in this demo. In the case of GridViewComboBoxColumn, it will be:

<UserControl.Resources>
        <Style x:Key="myEditorStyle" TargetType="telerik:RadComboBox">
            <Setter Property="EmptyText" Value="Empty" />      
        </Style>
    </UserControl.Resources>
 
<telerik:GridViewComboBoxColumn DataMemberBinding="{Binding ClubID}"
                                                SelectedValueMemberPath="ID"   
                                                EditorStyle="{StaticResource myEditorStyle}"
                                                ItemsSource="{Binding Clubs, Source={StaticResource MyViewModel}}">
  
Otherwise, if the underlying the property is nullable and there is not value for a particular item, just an empty cell will be created and you will be able to chose items afterwards. Please take a look at the sample attached for a reference. 

Regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Hazzard
Top achievements
Rank 1
answered on 02 Nov 2011, 09:47 PM
Thank you Maya for the code Maya. I reposted with i hope a more clear indication of what I'm attempting to do.
http://www.telerik.com/community/forums/silverlight/gridview/gridviewcomboboxcolumn---itemssourcebinding-viewmodel-sample-code.aspx
Class values flow
Tags
GridView
Asked by
Hazzard
Top achievements
Rank 1
Answers by
Maya
Telerik team
Hazzard
Top achievements
Rank 1
Share this question
or