Expand row details on clicking anywhere on the row

13 posts, 0 answers
  1. Ganesh Shivshankar
    Ganesh Shivshankar avatar
    78 posts
    Member since:
    Sep 2009

    Posted 18 Sep 2012 Link to this post

    Hi,

    I need to be able to expand collapse rowdetails by clicking anywhere on the row and not just using the toggle button column. How do I achieve this?

    Thanks.

    Cheers!
    Ganesh
  2. Ganesh Shivshankar
    Ganesh Shivshankar avatar
    78 posts
    Member since:
    Sep 2009

    Posted 18 Sep 2012 Link to this post

    Apparently this seems to work, but not sure if it is the best way to do it. Need to do the below in the SelectionChanged event of the GridView. I'm setting the SelectItem to null so that it triggers the selectionchanged again when again clicking the selected row.

    if (!e.AddedItems.Any()) e.Handled = true;
               else
               {
                   var gridView = (RadGridView) sender;
                   var selectedRow = gridView.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0]) as GridViewRow;
                   if (selectedRow == null) return;
                   selectedRow.DetailsVisibility = selectedRow.DetailsVisibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
                   gridView.SelectedItem = null;
               }

    Hope this helps someone else!

    Cheers!
    Ganesh
  3. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 19 Sep 2012 Link to this post

    Hello Ganesh,

    Thank you for sharing your solution.
    A better approach would be to subscribe for the MouseLeftButtonDown event and then execute your logic:

    void MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
                var row = (sender as TextBlock).ParentOfType<GridViewRow>();
                row.DetailsVisibility = row.DetailsVisibility == System.Windows.Visibility.Visible ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;
    }

    Greetings,
    Didie
    the Telerik team

    Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

  4. Ganesh Shivshankar
    Ganesh Shivshankar avatar
    78 posts
    Member since:
    Sep 2009

    Posted 19 Sep 2012 Link to this post

    Thanks Didie. 

    Do you mean hook up to MouseLeftButtonDown on the GridView or on each column? I hooked up the GridView but it doesnt seem to be firing the event?

    Regards,
    Ganesh
  5. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 20 Sep 2012 Link to this post

    Hello Ganesh,

    For example, if the GridView is named 'clubsGrid', then the code to subscribe for the event   
    would be:

    this.clubsGrid.AddHandler(FrameworkElement.MouseLeftButtonDownEvent, new System.Windows.Input.MouseButtonEventHandler(OnMouseDown), true);
     

    All the best,
    Didie
    the Telerik team

    Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

  6. Ganesh Shivshankar
    Ganesh Shivshankar avatar
    78 posts
    Member since:
    Sep 2009

    Posted 20 Sep 2012 Link to this post

    Didie,

    Is this not same as doing?

    clubGrid.MouseLeftButtonDown += OnMouseLeftButtonDown;
  7. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 20 Sep 2012 Link to this post

    Hello,

     With AddHandler approach you can handle even events marked as Handled (the third argument). 

    Regards,
    Vlad
    the Telerik team

    Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

  8. Ganesh Shivshankar
    Ganesh Shivshankar avatar
    78 posts
    Member since:
    Sep 2009

    Posted 20 Sep 2012 Link to this post

    Hi Vlad,

    I tried your approach. But the sender in the MouseDownEvent is GridView and not a TextBlock. So doing a ParentOfType on GridView is not going to give the GridViewRow. How do we resolve this?

    Ganesh
  9. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 20 Sep 2012 Link to this post

    Hi,

     You are right. In that case the sender is the RadGridView. That is why you should use the e.OriginalSource instead: 

    void MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
    var s = e.OriginalSource as FrameworkElement;
    var row = s.ParentOfType<GridViewRow>();
         if (row != null)
         {
       row.DetailsVisibility = row.DetailsVisibility == System.Windows.Visibility.Visible ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;
         }
    }

    All the best,
    Didie
    the Telerik team

    Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

  10. Ganesh Shivshankar
    Ganesh Shivshankar avatar
    78 posts
    Member since:
    Sep 2009

    Posted 20 Sep 2012 Link to this post

    Hi Didie,

    This works okay. But there's a bigger problem. Now when the row details is expanded and I click anything on the row details, it collapses the row details. In my case I have a tabcontrol in the rowdetails with some editable form. The moment I click on a button, for example, in it, it collapses the row details.
  11. Ganesh Shivshankar
    Ganesh Shivshankar avatar
    78 posts
    Member since:
    Sep 2009

    Posted 20 Sep 2012 Link to this post

    Hi Didie,

    Looking at my scenario it doesnt seem possible to achieve the behavior i want?

    Ganesh
  12. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 20 Sep 2012 Link to this post

    Hi,

    You can two additional checks in case you have another GridView in RowDetails. 
    For example:
    void MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                var s = e.OriginalSource as FrameworkElement;
                var cell = s.ParentOfType<GridViewCell>();
                if (cell != null)
                {
                    var row = cell.ParentOfType<GridViewRow>();
                    if (row != null && row.ParentOfType<GridViewRow>() == null)
                    {
                        row.DetailsVisibility = row.DetailsVisibility == System.Windows.Visibility.Visible ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;
                    }
                }
            }

    If this is not the case, indeed we cannot suggest you a better solution. 

    Greetings,
    Didie
    the Telerik team

    Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

  13. Ganesh Shivshankar
    Ganesh Shivshankar avatar
    78 posts
    Member since:
    Sep 2009

    Posted 20 Sep 2012 Link to this post

    Hi Didie,

    This looks perfect. Thanks. Will let you know in case I face any issues.

    Cheers!
    Ganesh
Back to Top