Updating Row when selecting RowDetails

10 posts, 0 answers
  1. Jason
    Jason avatar
    47 posts
    Member since:
    Jan 2013

    Posted 17 Jul 2014 Link to this post

    I have a project, which I'm updating to use the Telerik controls. As part of this, I have changed a standard ListView into a Telerik GridView. However, I have come across a problem.

    The gridview consists of a number of row bound to an observable list, each of which has a RowDetails template, showing another list of information in a ListBox. Double clicking on the items in the list box performs an action, based on the selected row in the gridview (using a binding on the SelectedItem).

    Previously, when selecting an item in the rowdetails, the listview grid would also select the parent row in the grid, thereby allowing our code to work. However, with the telerik gridview, selecting an item in the listbox DOES NOT select the parent row. As such, the selecteditem is either wrong or null, so the code fails.

    Is there a way to make the selected grid row reflect the currently focused listbox in the details?

    Cheers
    Jason
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 18 Jul 2014 Link to this post

    Hi Jason,

    You can subscribe for the RowActivated event of the parent (main) RadGridView and select the row which was just activated. Please note this event will be raised every time you double click on a row or anywhere in its related details. Then, select the parent row (which in the case is e.Row).
    For example:
    private void clubsGrid_RowActivated(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
    {
        if (!e.Row.IsSelected)
        {
            e.Row.IsSelected = true;
        }
    }

    How does this work for you?

    Regards,
    Didie
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. Mark
    Mark avatar
    8 posts
    Member since:
    Feb 2014

    Posted 29 Oct 2014 in reply to Dimitrina Link to this post

    It doesn't work at all!  Click or doubleclicking in the rowdetails area does not fire this event.
  4. Mark
    Mark avatar
    8 posts
    Member since:
    Feb 2014

    Posted 29 Oct 2014 in reply to Jason Link to this post

    Jason

    Did you find an answer for this?  I am having the same problem, I think.  I have multiple rows where row details are visible.  Selecting on an object in a row detail does not cause the selected row of the grid to change to the row detail that has focus.
  5. Jason
    Jason avatar
    47 posts
    Member since:
    Jan 2013

    Posted 30 Oct 2014 in reply to Mark Link to this post

    It's a while ago, but from what I can tell, the double click of the detail rows comes through as a double click on the grid, and hides the double click that happens on the list box.

    What I think I did was catch both the events on the listbox and the datagrid and did some check of the actual object that passed in, to determine whether it was a row or details section that was clicked on.

    Hope that points you in the right direction 
  6. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 30 Oct 2014 Link to this post

    Hello Mark,

    Are you double clicking on the row details or single clicking? As you say you are selecting on an object, I suppose it is a single click and so the RowActivated event will never be raised.

    In that case, you could capture the focus on the details and select the respective parent row.
    For example:

    private void playersGrid_GotFocus(object sender, RoutedEventArgs e)
    {
        var parentGrid = (e.OriginalSource as FrameworkElement).ParentOfType<RadGridView>();
        var parentRow = parentGrid.ParentRow;
        if (!parentRow.IsSelected)
        {
            parentRow.IsSelected = true;
        }
    }

    Let me know how this works for you.

    Regards,
    Dimitrina
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  7. Jason
    Jason avatar
    47 posts
    Member since:
    Jan 2013

    Posted 20 Nov 2014 in reply to Dimitrina Link to this post

    I've come back to this because it transpires I never actually solved it - I found another solution. However, I now need to get this working.

    The latest code sample doesn't work because ParentRow is set to null. Not even sure how that is supposed to work.

    I can get hold of the containing DetailsPresenter for the listbox in question, but can't see what to do with that.

  8. Mark
    Mark avatar
    8 posts
    Member since:
    Feb 2014

    Posted 20 Nov 2014 in reply to Jason Link to this post

    Jason

    The GotFocus solution is working for me.  Here is how I set it up.  The grid is setup with SelectionMode="Single".  I don't think the other settings are important.  My RowDetailsTemplate is somewhat complex as it is a whole input form.  I don't think that matters either.  You could probably hook an entire View/ViewModel combo and this solution would still work.  I'll be trying that later today as a matter of fact.

    In the code behind of the View containing the grid (yes, code behind.  It's all to control UI behavior so this shouldn't bother anyone):
    Constructor or Load:
         this.AddHandler(FrameworkElement.GotFocusEvent, new RoutedEventHandler(GotFocus));

    Then create the GotFocus method:
        private void GotFocus(object sender, RoutedEventArgs routedEventArgs)
        {
            var parentRow = (routedEventArgs.OriginalSource as FrameworkElement).ParentOfType<GridViewRow>();
            if (parentRow != null && !parentRow.IsSelected)
            {
                parentRow.IsSelected = true;
            }
        }

    GotFocus gets called whenever any object on the View gets focus, so it gets called a lot.  The method checks the object receiving focus and traverses parent objects until one of type GridViewRow is found or it reaches the top most object; the View itself.  If a GridViewRow is found, we set the focus.  If the RadGridView.SelectionMode is set to "Single", it should be obvious that the selection changes on the grid.  It's a brute force way of doing things, but it works quite well.

    If this isn't working for you, I'd like to see how you have your grid setup.  As I understand it, you have one RadGridView with a RowDetailsTemplate that contains a ListBox.  This solution should work for that setup.  Do you have more than one RadGridView in your control?

    Regards
    Mark
  9. Jason
    Jason avatar
    47 posts
    Member since:
    Jan 2013

    Posted 20 Nov 2014 in reply to Mark Link to this post

    Cheers, that works. Glad to see someone else who is sensible in their beliefs about code behind :)
    My original issue was that I had used the code from Dimitrina, which looks for a RadGridView parent, rather than a GridViewRow parent.
     
    I had actually solved the problem by catching the previewmousedown event on the listbox and raising a routed mousedown event on the parent, which I believe is the details presenter. Causes the gridview to handle the event as though the details section itself was clicked on, and hence handles extended selection
  10. Mark
    Mark avatar
    8 posts
    Member since:
    Feb 2014

    Posted 20 Nov 2014 in reply to Jason Link to this post

    Glad to hear it works!

    I thought about taking a similar approach to using PreviewMouseDown, but I have so many controls on this particular setup that wiring all the events in XAML would be painful.  Plus if I added a control later (that never happens, right?) and forget to hook it up...

    Good day!
    Mark
Back to Top