New to Telerik UI for WinForms? Download free 30-day trial

Column Selection in RadGridView

Environment

Product Version Product Author
2019.1.219 RadGridView for WinForms Desislava Yordanova

Description

This article demonstrates how to achieve column selection in RadGridView. For this purpose, it is necessary to keep the Shift key pressed and click the column's header. As a result all cells, belonging to this column, will be selected.

grid-column-selection

Solution

Create a derivative of the BaseGridBehavior class. Override its OnMouseDown method where, if the Shift key is pressed and the header cell element is clicked, you should iterate all rows and select programmatically the cells belonging to the clicked column.

When selecting multiple cells in RadGridView note that the selection should be wrapped in a MasterTemplate.SelectedCells.BeginUpdate - MasterTemplate.SelectedCells.EndUpdate block. Thus, the refresh operation will be done only once at the end of the operation but not with each selected cell.

Column selection


        public RadForm1()
        {
            InitializeComponent();
            radGridView1.MultiSelect = true;
            radGridView1.SelectionMode = GridViewSelectionMode.CellSelect;
            this.radGridView1.GridViewElement.GridBehavior = new CustomGridBehavior();
        }

        private class CustomGridBehavior : Telerik.WinControls.UI.BaseGridBehavior
        {
            private bool isShiftPressed = false;

            public bool IsShiftPressed
            {
                get
                {
                    return isShiftPressed;
                }
                set
                {
                    isShiftPressed = value;
                }
            }

            private SortDescriptorCollection currentSortDescriptors;

            public override bool ProcessKeyDown(KeyEventArgs keys)
            {
                isShiftPressed = (keys.Modifiers & Keys.Shift) == keys.Modifiers;

                return base.ProcessKeyDown(keys);
            }

            public override bool ProcessKeyUp(KeyEventArgs keys)
            {
                if (isShiftPressed)
                {
                    isShiftPressed = false;
                }

                return base.ProcessKeyUp(keys);
            }

            public override bool OnMouseDown(MouseEventArgs e)
            {
                var cellHeaderElement = this.GridControl.ElementTree.GetElementAtPoint(e.Location) as GridHeaderCellElement;

                if (cellHeaderElement != null && isShiftPressed)
                {
                    this.GridControl.ClearSelection();
                    this.GridControl.MasterTemplate.SelectedCells.BeginUpdate();
                    foreach (var row in this.GridControl.Rows)
                    {
                        row.Cells[cellHeaderElement.ColumnIndex].IsSelected = true;
                    }
                    this.GridControl.MasterTemplate.SelectedCells.EndUpdate(true);

                    return true;
                }
                else
                    return base.OnMouseDown(e);
            }
        }
In this article