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

ComboBox column and binding

7 Answers 276 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rayne
Top achievements
Rank 1
Rayne asked on 06 Sep 2011, 02:48 PM

My grid has a comboBox column. But my binding isn't quite working the way I want. Here is my code:

<telerik:GridViewComboBoxColumn Header="Action" UniqueName="Action"
          DataMemberBinding="{Binding ActionID, Mode=TwoWay, ValidatesOnDataErrors=True}"
          SelectedValueMemberPath="ActionID" DisplayMemberPath="Name"
          ItemsSource="{Binding DataContext.Actions, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl,AncestorLevel=2}}"
          IsComboBoxEditable="True">
 <telerik:GridViewComboBoxColumn.CellTemplate>
          <DataTemplate>
                 <TextBlock Text="{Binding Action.Name}" />
           </DataTemplate>
  </telerik:GridViewComboBoxColumn.CellTemplate>
</telerik:GridViewComboBoxColumn>

When the row loads, the value for this comboBox displays. But when the user changes it or selects a value for a new row, it doesn't display the value until the row is committed. I know this is because I'm displaying the value of Action.Name, but binding the selected value to ActionID.

Is there any way I can get it to show the Name property before its been committed? I could if I don't use a CellTemplate and just use the comboBox, but then there are some records that have an action that isn't a current available option in the drop down. So if I don't use the CellTemplate, then those will display blank because there isn't a matching item in the drop down. Make sense?

How do I accomodate both?

7 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 07 Sep 2011, 08:20 AM
Hi Rayne,

Would you please take a look at this forum thread ? Do you want to achieve something similar ? 

All the best,
Maya
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Rayne
Top achievements
Rank 1
answered on 07 Sep 2011, 03:07 PM
Well, sort of. My ComboBox column is bound to a collection of Actions. These actions for archival purposes have an Active Flag. So actions that are no longer available have been "de-activated".

When the grid binds to the collection, it's displaying the child property Action.Name in the cell using the CellTemplate defined. The comboBox column is binding to the ActionID value of the data (not the Action navigational property). So once the user selects an item from the combobox, the value is blank, because the celltemplate displays Action.Name and until the ActionID is committed, there is no Action property.

If I remove the CellTemplate Data Template, then it works. It displays the Name value associated with the selected ActionID. But for existing rows, there may be one that has and Action chosen that has now been "de-activated" so it's not longer in the collection that the combobox uses. So that row shows a blank (no matching record in the combobox itemssource).

What I need is to be able to display those actions regardless of whether they are in the ComboBox ItemsSource or not (hence the reason for the CellTemplate binding to Action.Name), but then it doesn't show up when the row is in EditMode (once an Action has been selected and the user moves to next cell in row).
0
Maya
Telerik team
answered on 13 Sep 2011, 09:00 AM
Hi Rayne,

Indeed, generally, you cannot display a value in GridViewComboBoxColumn that is not in its ItemsSource. Will it be possible to share some code-snippets about the implementation of your two business objects - the one for the grid and the one for the column and the definitions of your column ? 

 

Best wishes,
Maya
the Telerik team

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

0
Rayne
Top achievements
Rank 1
answered on 13 Sep 2011, 01:39 PM

Action (this is the ItemsSource of the ComboBoxColum):

  • ActionID
  • Name
  • Active
  • RowVersion

Over time, some actions might become inactive as business rules change. So I'm using a Linq query to populate an ObservableColleciton where Active is true. This is the list of available choices the user can pick from.

_context.Actions.Where(a => a.Active).OrderBy(a => a.Name);

The business model for the row has a scalar property ActionID and a navigational property Action, where the ActionID property is the foreign key association for the Action property. The business model is in Entity Framework. 

I want to display the Action regardless of whether it is currently an available choice or not. If they choose to edit that record, then they have to choose a different Action from the list of valid choices.

There's got to be a way to do it. I had tried setting a CellTemplate to show Action.Name so that it displays the existing value. It worked for existing rows. But for new rows the cell is blank while the row is in edit mode, because it doesn't assign the Action property based on the ActionID (foreign key) property until the row is committed. So the user can't see what action they selected until after they are done editing the row.

0
Maya
Telerik team
answered on 19 Sep 2011, 09:17 AM
Hello Rayne,

Would you try to handle SelectionChanged event of RadComboBox and commit the changes:

this.AddHandler(RadComboBox.SelectionChangedEvent, new SelectionChangedEventHandler(OnSelectionChanged));
  
  
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.RemovedItems.Count != 0)
    {
        //this ensures a selection from the drop down is performed.
           this.playersGrid.CommitEdit();
    }
}
 
Thus once you select an item from the drop down, the change will be committed.
Will that approach work in your case ?
Greetings,
Maya
the Telerik team

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

0
Kevin
Top achievements
Rank 1
answered on 14 Jan 2016, 01:20 PM
Do you realize how horribly wrong your answer goes against MVVM? If it were a Winform application this would be an almost acceptable answer, but to be honest It really doesn't surprise me. 
0
Ivan Ivanov
Telerik team
answered on 19 Jan 2016, 09:54 AM
Hello,

Although MVVM is considered to be the standard XAML UI architectural pattern, following it completely remains a matter of personal choice. Most of the standard non-MVVM techniques have valid MVVM-friendly counterparts. We prefer to provide the most basic solution that focuses on the problem itself, instead of overengineering it without having the full context of the client's project. These solutions are left open for further interpretations and we are always glad to discuss them with our clients.
I am attaching a modified version of the discussed approach that uses the Evend-To-Command pattern to avoid direct event handling in the view's partial class.

Regards,
Ivan Ivanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Rayne
Top achievements
Rank 1
Answers by
Maya
Telerik team
Rayne
Top achievements
Rank 1
Kevin
Top achievements
Rank 1
Ivan Ivanov
Telerik team
Share this question
or