unselect a selected row

3 posts, 0 answers
  1. Chris Thierry
    Chris Thierry avatar
    138 posts
    Member since:
    Jun 2010

    Posted 29 Jun 2011 Link to this post

    Hi,
    I have a problem trying to deselect a selected row, I found this but is not working:

    RadGridViewOrgUnit.AddHandler(GridViewCell.MouseLeftButtonDownEvent,
                    new MouseButtonEventHandler(RadGridViewOrgUnit_MouseLeftButtonDown), true);

    In the method I have this:

    private void RadGridViewOrgUnit_MouseLeftButtonDown(object s, MouseButtonEventArgs args)
            {
                var senderElement = (FrameworkElement)args.OriginalSource;
                var clickedRow = senderElement.ParentOfType<GridViewRow>();
     
                if (clickedRow != null)
                {
                    if (this.OrganizationUnitUC.RadGridViewOrgUnit.SelectedItems.Count > 0)
                    {
                        IdValue itemDate = ((IdValue)((RadGridView)s).SelectedItem);
      
                            if (itemDate.ID == this.OrganizationUnitUC.OrganizationUnitSelectedItemPrevious)
                            {
                                this.OrganizationUnitUC.SelectedItems.Clear()
                            }
                            else
                            {
                                //Do something else...
                            }                   
                    }               
                }           
            }

    When I run this, and I do click in the same row, the row continue selected, but if I put a breakpoint in the Clear() and then continue with the code, is working fine!, if no breakpoint, the row continue selected, is there any problem with this event? is there any other way to do it?
    I need a solution because I don't want to change the RadGridView for another control.
    Thank you.

    I already checked this http://www.telerik.com/community/forums/silverlight/gridview/how-to-deselect-a-row-when-it-is-clicked.aspx but is not my case. I need only one row selected, If I click in another row, has to do the same effect, if I click on the same row, it should deselect the row.
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 30 Jun 2011 Link to this post

    Hello Chris Thierry,

    Generally, I would recommend you to benefit from the SelectionMode property of the grid set to "Multiple". Still, if you want to work mandatory with single selection, you may do the following:

    private object itemToSelect;
     
            void clubsGrid_SelectionChanging(object sender, SelectionChangingEventArgs e)
            {
                if (e.AddedItems.Count > 0)
                {
                    itemToSelect = e.AddedItems[0];
                }
            }
     
            private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                var senderElement = (FrameworkElement)e.OriginalSource;
                var clickedRow = senderElement.ParentOfType<GridViewRow>();
     
                if (clickedRow != null && this.itemToSelect == null)
                {
                    this.clubsGrid.SelectedItems.Clear();              
                }
     
                itemToSelect = null;
            }

    I am sending you a sample project so that you may test the functionality.


    Regards,
    Maya
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Chris Thierry
    Chris Thierry avatar
    138 posts
    Member since:
    Jun 2010

    Posted 30 Jun 2011 Link to this post

    Thank you!
Back to Top