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

unselect a selected row

2 Answers 111 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chris Thierry
Top achievements
Rank 1
Chris Thierry asked on 29 Jun 2011, 07:12 PM
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 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 30 Jun 2011, 12:46 PM
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
0
Chris Thierry
Top achievements
Rank 1
answered on 30 Jun 2011, 07:59 PM
Thank you!
Tags
GridView
Asked by
Chris Thierry
Top achievements
Rank 1
Answers by
Maya
Telerik team
Chris Thierry
Top achievements
Rank 1
Share this question
or