GridView deselect row on CTRL + Click

1 Answer 208 Views
GridView
Jure
Top achievements
Rank 2
Iron
Iron
Iron
Jure asked on 21 Jun 2021, 01:36 PM

As the title suggest, how can I deselect currently selected row if it's previously selected? I have MultiSelect set to False. Ctrl + click doesn't work.

 

J

1 Answer, 1 is accepted

Sort by
1
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 24 Jun 2021, 06:22 AM

Hello, Jure,  

I would like to note that when you click a cell for the first time, the row gets selected (GridViewSelectionMode.FullRowSelect). If you click again the same cell, it is expected to enter edit mode. Hence, the row remains selected.

If you need to deselect the already selected row after clicking it, you need to customize this desired behavior. RadGridView manages user mouse and keyboard input over its rows by GridRowBehavior. Depending on the row type, RadGridView introduces different behaviors:

https://docs.telerik.com/devtools/winforms/controls/gridview/rows/row-behaviors

I have prepared a sample code snippet for your reference which result is illustrated in the attached gif file: 

        public RadForm1()
        {
            InitializeComponent();

            this.radGridView1.Columns.Add("Id");
            this.radGridView1.Columns.Add("Name");
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            for (int i = 0; i < 10; i++)
            {
                this.radGridView1.Rows.Add(i, Guid.NewGuid().ToString());
            }

            this.radGridView1.MultiSelect = false;
            this.radGridView1.SelectionMode = GridViewSelectionMode.FullRowSelect;
            
            //register the custom row  behavior
            BaseGridBehavior gridBehavior = radGridView1.GridBehavior as BaseGridBehavior;
            gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
            gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomGridDataRowBehavior());
        }

        public class CustomGridDataRowBehavior : GridDataRowBehavior
        {
            bool isSelected = false;

            protected override bool OnMouseDownLeft(MouseEventArgs e)
            {
                GridCellElement cellUnderMouse = this.GridViewElement.ElementTree.GetElementAtPoint(e.Location) as GridCellElement;
                if (cellUnderMouse != null)
                {
                    isSelected = cellUnderMouse.RowInfo.IsSelected;
                }
                return base.OnMouseDownLeft(e);
            }

            protected override bool OnMouseUpLeft(MouseEventArgs e)
            {
                GridCellElement cellUnderMouse = this.GridViewElement.ElementTree.GetElementAtPoint(e.Location) as GridCellElement;
                
                bool result = base.OnMouseUpLeft(e);
                if (this.GridViewElement.ActiveEditor != null)
                {
                    this.GridViewElement.CloseEditor();
                }
               
                if (cellUnderMouse != null)
                {
                    if (isSelected == cellUnderMouse.RowInfo.IsSelected)
                    {
                        cellUnderMouse.RowInfo.IsSelected = !cellUnderMouse.RowInfo.IsSelected;
                        if (!cellUnderMouse.RowInfo.IsSelected)
                        {
                            cellUnderMouse.RowInfo.IsCurrent = false;
                        }
                    }
                }
                return result;
            }
        }
I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Jure
Top achievements
Rank 2
Iron
Iron
Iron
commented on 24 Jun 2021, 09:39 AM

Great! Thanks Dess!
Tags
GridView
Asked by
Jure
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or