If you have used RadGridView with a GridViewComboBoxColumn, you know it does not support SelectionChangedEvent directly. There are many scenarios where you want to handle some validation or perform some action when the user selects an item in a combo box but the absense of SelectionChangedEvent for GridViewComboBoxColumn makes this really hard. Good news is, there is a solution. Here it is, see if it helps.
Let’s say you have a RadGridView defined like this:
<telerik:RadGridView x:Name="myGridView"> <telerik:RadGridView.Columns> <telerik:GridViewComboBoxColumn Header="Page" UniqueName="column1" /> </telerik:RadGridView.Columns> </telerik:RadGridView>
In your code behind, preferably in the Loaded event, add this code:
this.AddHandler(RadComboBox.SelectionChangedEvent, new Telerik.Windows.Controls.SelectionChangedEventHandler(ComboBox_SelectionChanged));
Add an event for Selection changed event. The name of your method and the event above should match.
void ComboBox_SelectionChanged(object sender, RadRoutedEventArgs args) { RadComboBox comboBox = (RadComboBox)args.OriginalSource; if (comboBox.SelectedValue == null) return; // Get the current cell GridViewCell cell = myGridView.CurrentCell; // Handle the situation where a cell is null. It can be when you are adding a new row if (cell == null) { return; } GridViewRowItem row = cell.ParentRow; GridViewColumn col = cell.Column; // This if block here is to handle only the combo box column desired. You can // have multiple combobox columns and this event will be triggered for all of them. // Handle what is required and ignore the rest if (col.UniqueName != "column1") // Unique name that you specified in the XAML { return; } // YourDataObject is whatever data object you are using YourDataObject item = comboBox.DataContext as YourDataObject; //SelectedItem returns object. Convert it to whatever your binding object type is object selectedItem = comboBox.SelectedItem; // Now that you have your selected item, you can do whatever you want. // In case you want to disable other cells on this row, just use row.Cells property. }
That’s it. You are all set now.
Some points to note:
1. SelectionChanged event will be triggered for all combo box columns and combo boxes you have. You need to find out if the deisred combo box column is changed.
2. This event is triggered not only after the selection is changed, but also when the user just clicks on the combo box. There is a solution to handle this, but that’s another story altogether.
3. This was tried with VS 2010 and Silverlight 4.