ComboBox column and binding

6 posts, 0 answers
  1. Rayne
    Rayne avatar
    230 posts
    Member since:
    Sep 2010

    Posted 06 Sep 2011 Link to this post

    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?

  2. Maya
    Admin
    Maya avatar
    3769 posts
    Member since:
    Sep 2012

    Posted 07 Sep 2011 Link to this post

    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 >>

  3. Rayne
    Rayne avatar
    230 posts
    Member since:
    Sep 2010

    Posted 07 Sep 2011 Link to this post

    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).

  4. Maya
    Admin
    Maya avatar
    3769 posts
    Member since:
    Sep 2012

    Posted 13 Sep 2011 Link to this post

    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 >>

  5. Rayne
    Rayne avatar
    230 posts
    Member since:
    Sep 2010

    Posted 13 Sep 2011 Link to this post

    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.

  6. Maya
    Admin
    Maya avatar
    3769 posts
    Member since:
    Sep 2012

    Posted 19 Sep 2011 Link to this post

    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 >>

Back to Top